[英]CodeChef wrong answer in Factorial task
我正在尝试解决CodeChef问题“小析因” 。 任务是计算给定数字的阶乘。 我有以下代码,我已经检查了很多次。 对我来说,它给出正确的输出,但是当我尝试将其上传到CodeChef时,它给出错误的错误答案。
import java.util.Scanner;
class SmallFactorial {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int iterations = sc.nextInt();
int[] myArray = new int[iterations];
int result = 1;
for(int b = 0; b < iterations; b++) {
int n = sc.nextInt();
if (n >= 1 && n <= 100) {
for (int i = 1; i <= n; i++) {
result = result * i;
}
myArray[b] = result;
result = 1;
}
}
for(int z = 0; z < myArray.length; z++){
System.out.println(myArray[z]);
}
sc.close();
}
}
我看不到致命的错误,但是if条件可能会出现问题,因为0! = 1并且您没有解决这个问题,或者您为控制此问题的程序提供了错误的输出语法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.