编写Java应用程序,求40的阶乘(要求输出结果的每一位)。
答案:2 悬赏:30 手机版
解决时间 2021-11-14 03:54
- 提问者网友:我的未来我做主
- 2021-11-13 16:30
编写Java应用程序,求40的阶乘(要求输出结果的每一位)。
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-11-13 17:43
import java.math.BigInteger;
import java.util.*;
public class ac
{
protected static ArrayList alist=new ArrayList();
static
{
alist.add(BigInteger.valueOf(1));
}
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("40!="+factorial(40));
}
}
import java.util.*;
public class ac
{
protected static ArrayList alist=new ArrayList();
static
{
alist.add(BigInteger.valueOf(1));
}
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("40!="+factorial(40));
}
}
全部回答
- 1楼网友:底特律间谍
- 2021-11-13 18:05
long result = 1;
for (int i = 1; i <= 40; i++) {
result=result*i;
}
System.out.println(result);
for (int i = 1; i <= 40; i++) {
result=result*i;
}
System.out.println(result);
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯