计数100的阶乘,结果取近似值或者精确值都可以
java程序实现

解决方案 »

  1.   


    public static void main(String[] args){
                    Scanner scan=new Scanner(System.in);
            System.out.println("请输入一个整数:");
    long n=scan.nextLong();
    long sum=1;
    for(long i=1;i<=n;i++){
    sum*=i;
    }
    System.out.println("n的阶乘是:"+sum);
    }
      

  2.   

    要的是100的   结果会超出long的范围
      

  3.   


    import java.math.BigInteger;
    import java.util.ArrayList;
     
    public class NewClass {    protected static ArrayList alist = new ArrayList();    static {
            alist.add(BigInteger.valueOf(1));
        }    /**
         * Creates a new instance of factorial
         */
        public static synchronized BigInteger factorial(int x) {
            if (x < 0) {
                throw new IllegalArgumentException("x must be non-negative.");
            }
            for (int size = alist.size(); size <= x; size++) {
                BigInteger lastfact = (BigInteger) alist.get(size - 1);
                BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
                alist.add(nextfact);
            }
            return (BigInteger) alist.get(x);
        }    public static void main(String[] args) {
            
                System.out.println("100 != " + factorial(100));
           
        }
    }
      

  4.   

    run:
    100 != 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
    成功生成(总时间:1 秒)