我遇到了这样一个问题:
编写程序,对小于给定数N的质数按数字排序。如果N是40,则输出应该是11、13、17、19、2、23、29、3、31、37、39、5和7。注意:限制内存使用。
获取质数很容易。但我无法想出一种有效的整数数组词典排序的方式。
编写程序,对小于给定数N的质数按数字排序。如果N是40,则输出应该是11、13、17、19、2、23、29、3、31、37、39、5和7。注意:限制内存使用。
获取质数很容易。但我无法想出一种有效的整数数组词典排序的方式。
public static void getPrimeNumbers(int limit) {
for (int i=2; i<=limit; i++) {
if(isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int number) {
for(int j=2; j<number; j++) {
if(number%j==0) {
return false;
}
}
return true;
}
public static void lexographicSorting() {
int[] input = {2,3,5,7,11,13,17,19};
int[] output = {};
for (int i=0; i<input.length; i++) {
for(int j=0; j<input.length; j++) {
////Stuck at this part.
}
}
}
The method sort(int[]) in the type Arrays is not applicable for the arguments (int[], new Comparator<Integer>(){})
错误。 - Himanshu Yadav