Factorial of large numbers bjfu1005 java实现大数阶乘
2013-01-19 18:48
351 查看
描述
Factorial, as we all know, is the product of all the positive integers from one up to and including a given integer. For example, factorial zero is assigned the value of one; factorial
four is 1 × 2 × 3 × 4. Symbol: N! The N is the given integer. But it is hard to calculate the factorial when the N is large. Ben wrote a program to calculate N!(N<10000) when he was in grade one. So try it!
输入
The input consists of multiple test cases. One N (0<=N<=10000,integer) in a line for each test case. Process to the end of file.
输出
For each test case, print the value of N! in a single line.
样例输入
5
10
20
100
样例输出
120
3628800
2432902008176640000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
我的症结还在于,不会熟练的在int和Biginteger之间转换。。
Factorial, as we all know, is the product of all the positive integers from one up to and including a given integer. For example, factorial zero is assigned the value of one; factorial
four is 1 × 2 × 3 × 4. Symbol: N! The N is the given integer. But it is hard to calculate the factorial when the N is large. Ben wrote a program to calculate N!(N<10000) when he was in grade one. So try it!
输入
The input consists of multiple test cases. One N (0<=N<=10000,integer) in a line for each test case. Process to the end of file.
输出
For each test case, print the value of N! in a single line.
样例输入
5
10
20
100
样例输出
120
3628800
2432902008176640000
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner cin=new Scanner(System.in); int i; while (cin.hasNext()){ i=cin.nextInt(); getBigFactorial(i); } cin.close(); } public static void getBigFactorial(int n) { if (n==0) System.out.println("0"); BigInteger result=new BigInteger("1"); for (;n>0;n--) result=result.multiply(new BigInteger(n+"")); System.out.println(result); } }
我的症结还在于,不会熟练的在int和Biginteger之间转换。。
相关文章推荐
- BJFU 1005——Factorial of large numbers
- Java实现大数的阶乘(数组存放数据)
- 【LeetCode-面试算法经典-Java实现】【172-Factorial Trailing Zeroes(阶乘尾后0的数目)】
- 大数据阶乘(The factorial of large data)
- java-用数组实现大数阶乘
- 【LeetCode-面试算法经典-Java实现】【201-Bitwise AND of Numbers Range(范围数位与结果)】
- 大数阶乘Java实现
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- bag-of-words model的java实现
- java用三种方法实现阶乘n!
- 递归求10的阶乘(Java实现)
- zoj Fibonacci Numbers ( java , 简单 ,大数)
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
- 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】
- JAVA 大数阶乘
- 51 Nod 1008 N的阶乘 mod P【Java大数乱搞】
- 大整数相乘和大数阶乘实现
- LeetCode-java实现-T2-Add Two Numbers
- java实现50000的阶乘
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】