标签(空格分隔): 九度OJ
原题地址:http://ac.jobdu.com/problem.php?pid=1076
输入一个正整数N,输出N的阶乘。
正整数N(0<=N<=1000)
输入可能包括多组数据,对于每一组输入数据,输出N的阶乘
同样是大整数的题,显然我们仍然使用BigInteger类!这个题的要求时间是3秒,但是Java一般的时间会扩充两秒,最终这个程序花了3250 ms AC.
第一遍出现错误是因为忘了赋值了。
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { BigInteger n = scanner.nextBigInteger(); BigInteger answer = BigInteger.ONE; BigInteger one = BigInteger.ONE; while (n.compareTo(one) > 0) { answer = answer.multiply(n);//别忘记赋值 n = n.subtract(one); } System.out.println(answer.toString()); } } }2017 年 3 月 8 日