【发布时间】:2014-10-20 09:54:50
【问题描述】:
这里是代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class prob_c {
public static void main(String args[]) throws IOException {
Scanner x = null;
try {
x = new Scanner(new File("prob_c.in"));
} catch (FileNotFoundException ex) {
System.out.print("File not found!");
}
for (int a = 0; a < 10; a++) {
int counter = 0;
int n = x.nextInt();
int r = x.nextInt();
counter++;
long res = 1;
res = getFact(n) / (getFact(n - r) * getFact(r));
System.out.println(res);
}
}
public static long getFact(int j) {
long f = 1;
for (int i = j; i >= 1; i--) {
f *= i;
}
return f;
}
}
【问题讨论】:
-
就像编写一个检查零的 if 条件一样简单。你没有办法阻止它。
-
这不是一个坏问题。我相信明显被零除实际上是由计算阶乘时的整数溢出引起的。不是通过实际尝试除以零。遗憾的是,每个人都宁愿投反对票或近距离投票也不愿发布有关整数溢出的有用答案并使用
BigInteger而不是long。 -
你能写出触发异常的输入吗? (prob_c.in 的内容)
-
5 3 5 2 5 5 4 2 10 7 20 9 52 5 100 0 75 30 1 1 28 18 每行两个数字
标签: java exception combinations