计数100的阶乘,结果取近似值或者精确值都可以
java程序实现
------解决方案--------------------------------------------------------
[code=Java][/code]
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);
}
------解决方案--------------------------------------------------------
- Java code
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)); }}