[JAVA][HDU 1042][N!]

来源:互联网 发布:linux ftp用户及目录 编辑:程序博客网 时间:2024/06/15 10:37
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.io.BufferedInputStream;  
  2. import java.math.BigInteger;  
  3. import java.util.Scanner;  
  4.   
  5. public class Main {  
  6.   
  7.     public static BigInteger f(int n) {  
  8.         BigInteger sum = new BigInteger("1");  
  9.         while (n > 1) {  
  10.             sum = sum.multiply(BigInteger.valueOf(n--));  
  11.         }  
  12.         return sum;  
  13.     }  
  14.   
  15.     public static void main(String[] args) {  
  16.         Scanner sc = new Scanner(new BufferedInputStream(System.in));  
  17.         while (sc.hasNext()) {  
  18.             BigInteger x = f(sc.nextInt());  
  19.             System.out.println(x);  
  20.         }  
  21.     }  
  22. }  


当年用C硬是没写出来,貌似可以用到64b的int解决,用JAVA就简单多了

只要知道BigInteger的使用方式就可以了,注意BigInteger类里面的return value就行,

0 0