程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> biginteger-Java中n!的代碼看不懂啊,求解釋

biginteger-Java中n!的代碼看不懂啊,求解釋

編輯:編程綜合問答
Java中n!的代碼看不懂啊,求解釋

import java.math.BigInteger;
import java.util.*;
public class Main{
protected static ArrayList table = new ArrayList();
static
{
table.add(BigInteger.valueOf(1));
}
public static synchronized BigInteger factorial(int x)
{
for (int size = table.size(); size <= x; size++)
{
BigInteger lastfact = (BigInteger) table.get(size - 1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.print(factorial(n));
}
}

請問這些都是什麼意思……新人不是很理解。謝謝!

最佳回答:


multiply 是乘運算
table 是關鍵,先求 table 有幾個元素,然後加 1.例如: table 初始化只有一個元素,數值為 1. 一次運行: table 只有一個元素,加上 1 後為 2;初始化數值與 2 做乘法。二次運行: table 有兩個元素,加 1 後為 3;上次乘的結果,再乘 3...,依次類推完成 N 的階乘的計算。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved