求满足1+2!+3!+...+n!<=A的最大整数n,代码如下
输入2
10
9
43954713
输出
1
3
3
11
我的代码如下:结果可以输出来并且正确,但是提交到OJ上时却错误,这有可能是什么错误?
import java.util.*;
public class Main {
public static int fun(int n) {
int s = 1;
for (int i = 1; i <= n; i++) {
s *= i;
}
return s;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int a, sum = 0, n = 0;
while (cin.hasNext()) {
a = cin.nextInt();
if (a == 0) {
break;
} else {
for (; n <= a; n++) {
sum += fun(n);
if (sum >= a) {
break;
}
}
System.out.println(n);
}
}
}
}