Java 常见例子

目标:编写一个 Java 程序来查找给定自然数的所有不同因数(除数)。下面给出几个数字的约数:

Number: 10
 Divisors: 1 2 5 10

 Number: 15
 Divisors: 1 3 5 15

 Number: 100
 Divisors: 1 2 4 5 10 20 25 50 100 

方法1:使用迭代

基本方法之一是从1迭代到n 并在每次迭代中检查该数字是否整除 n。如果相除则打印它。

public class MyClass {
  //打印一个数字的所有除数的方法
  static void printDivisors(int n) {
    System.out.print("Divisors of " + n + " are: ");
    for(int i = 1; i <= n; i++) {
      if(n%i == 0)
        System.out.print(i + " ");
    }
    System.out.println();
  }

  public static void main(String[] args) {
    printDivisors(10);
    printDivisors(50);
    printDivisors(100);
  }
} 

上面的代码将给出以下输出:

Divisors of 10 are: 1 2 5 10 
Divisors of 50 are: 1 2 5 10 25 50 
Divisors of 100 are: 1 2 4 5 10 20 25 50 100 

方法2:优化代码

而不是检查给定数字从1n的整除性,检查直到n的平方根。对于大于n的平方根的因子,必须在1n的平方根的范围内存在已经检查过的更小的因子>.

import java.lang.Math;

public class MyClass {
  //打印一个数字的所有除数的方法
  static void printDivisors(int n) {
    System.out.print("Divisors of " + n + " are: ");
    //从1循环到Math.sqrt(n)
    for(int i = 1; i <= Math.sqrt(n); i++) {
      if(n%i == 0) {
        if(n/i == i)
          System.out.print(i + " ");
        else
          System.out.print(i + " " + n/i + " ");
      }
    }
    System.out.println();
  }

  public static void main(String[] args) {
    printDivisors(10);
    printDivisors(50);
    printDivisors(100);
  }
} 

上面的代码将给出以下输出:

Divisors of 10 are: 1 10 2 5 
Divisors of 50 are: 1 50 2 25 5 10 
Divisors of 100 are: 1 100 2 50 4 25 5 20 10 

方法3:优化代码并排序结果

在上一个方法中,结果以不规则的方式产生(成对打印 - 小数字和大数字)。可以通过存储较大的数字来对结果进行排序并稍后打印它们。考虑下面的示例:

import java.lang.Math;

public class MyClass {
  //打印一个数字的所有除数的方法
  static void printDivisors(int n) {
    System.out.print("Divisors of " + n + " are: ");
    
    //创建一个数组来存储更大的数字
    int[] arr = new int[n];
    int j = 0;
    
    //从1循环到Math.sqrt(n)
    for(int i = 1; i <= Math.sqrt(n); i++) {
      if(n%i == 0) {
        if(n/i == i)
          System.out.print(i + " ");
        else {
          System.out.print(i + " ");
          //存储对的大数
          arr[j++] = n/i;        
        }
      }
    }

    //打印存储的大量对
    for(int i = j - 1; i >= 0; i--)
      System.out.print(arr[i] + " ");
    System.out.println();
  }

  public static void main(String[] args) {
    printDivisors(10);
    printDivisors(50);
    printDivisors(100);
  }
} 

上面的代码将给出以下输出:

Divisors of 10 are: 1 2 5 10 
Divisors of 50 are: 1 2 5 10 25 50 
Divisors of 100 are: 1 2 4 5 10 20 25 50 100 

方法 4:另一个优化代码

生成得到排序后的结果,我们可以从 1 迭代到 n 的平方根,并打印除 n 的数字。之后我们可以迭代(以相反的顺序)并打印除n的所有数字的商。

import java.lang.Math;

public class MyClass {
  //打印一个数字的所有除数的方法
  static void printDivisors(int n) {
    System.out.print("Divisors of " + n + " are: ");
    //从1循环到Math.sqrt(n)
    int i;
    for(i = 1; i <= Math.sqrt(n); i++) {
      if(n%i == 0) 
        System.out.print(i + " ");
      
      //处理完全平方数
      if(n/i == i) {
        i--; break;
      }
    }

    for(; i >= 1; i--) {
      if(n%i == 0) 
        System.out.print(n/i + " ");
    }  
    System.out.println();
  }

  public static void main(String[] args) {
    printDivisors(10);
    printDivisors(50);
    printDivisors(100);
  }
} 

上面的代码将给出以下输出:

Divisors of 10 are: 1 2 5 10 
Divisors of 50 are: 1 2 5 10 25 50 
Divisors of 100 are: 1 2 4 5 10 20 25 50 100