我代码的输出结果和测试用例也是一样的,然后也有考虑到L<2的情况,不知哪里出了问题,故求助各位大佬
import java.util.*;
import static java.lang.Math.ceil;
import static java.lang.Math.max;
public class Main {
public static void main(String[] args) {
// 创建一个Scanner对象,用于接收用户输入
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int count=0;
int sum=0;
if (n<2){
System.out.println(0);
return;
} else if (n==2) {
System.out.println(2+"\n"+"1");
return;
}
for (int i = 2; i <= n; i++) {
// 判断i是否为质数
if (isZhi(i)&&(sum+i)<=n){
System.out.println(i);
sum+=i;
count++;
// System.out.println("sum="+sum);
}
}
System.out.println(count);
}
private static boolean isZhi(int i) {
// 判断i是否为2、3、5
if (i==2||i==3||i==5){
return true;
}
// 判断i是否为偶数、5的倍数、3的倍数
if (i %2==0|| i %5==0|| i %3==0){
return false;
} else {
boolean flag=true;
// 遍历2到i的平方根之间的数,判断i是否为质数数
for (int i1 = 2; i1 < Math.sqrt(i); i1++) {
if (i %i1==0){
flag=false;
break;
}
}
return flag;
}
}
}