【发布时间】:2017-08-17 16:11:40
【问题描述】:
import java.util.Scanner;
public class MathsHacker {
public static int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
/*
I checked for 1 test case giving an input=5277
it results in exception :
Exception in thread "main" java.lang.ArithmeticException: / by zero
*/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int T =in.nextInt();
for(int a0 = 0; a0 < T; a0++)
{
int N = in.nextInt();
if(N==1)
{
System.out.println("0");
}
else
{
int res=fact(N)/(fact(N-2)*fact(2));
System.out.println(res);
}
}
}
【问题讨论】:
-
5277 的教员对于一个 int 来说太大了。
-
@TobiasGeiselmann 那么我应该在我的代码中更改什么。只需将 int 更改为 long?
-
不,
long也不够大。你可以使用BigInteger。