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 的阶乘的计算。

解决方案二:

关键是看懂BigInteger这个类,它实现了大数的运算和存储。然后阶乘本身没什么难理解的。

解决方案三:

能 不能有个代码规范。静态代码块是最先执行的,写这个的目的就是给size 赋初值1,后面的你单步调试一下就明白了

解决方案四:

就是求大数的阶乘,主要是BigInteger

时间: 2024-08-04 13:51:22

biginteger-Java中n!的代码看不懂啊,求解释的相关文章

刚学C#,有句代码看不懂,求解释

问题描述 privatePlayingCard[,]cardPack;这个privatePlayingCard,能理解,是定义一个PlayingCard的二维数组.后面的呢?没有数据类型的变量? 解决方案 解决方案二:应该是对象数组...intstring能做数据类型自己的写的类也可以了..解决方案三:引用1楼net_java_dram的回复: 应该是对象数组...intstring能做数据类型自己的写的类也可以了.. 呵呵,没注意,原来有个空格隔开,这回明白了PlayingCard是个类,定义

ormat essage-C++代码看不懂,求大神指点

问题描述 C++代码看不懂,求大神指点 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL ); 这段代码不太清楚什么意思,尤其是 (LPTSTR) &lpMsgBuf这句,请大神们指点指点 解决方案 把错误转换成到lpMs

算法 递归 数据结构-求解释代码,添加注释或者画图解释(在网上看到的求组合的递归算法,绕晕头了看不懂,求解释)

问题描述 求解释代码,添加注释或者画图解释(在网上看到的求组合的递归算法,绕晕头了看不懂,求解释) public class Test1 { public static void main(String[] args) { select(2); } private static void select(int k) { char[] result = new char[k]; subselect(0 1 result k); } private static void subselect(int

jsp 网站开发-想要做一个网站上的图片下载的链接,有一行代码看不懂,求指导

问题描述 想要做一个网站上的图片下载的链接,有一行代码看不懂,求指导 d3.select(this).attr("href", "data:image/svg+xml;charset=utf-8;base64," + btoa(unescape(encodeURIComponent( svg.attr("version", "1.1") .attr("xmlns", "http://www.w3.

C#初学者 代码看不懂,求大神,用了DBhelper之后两段代码是怎么实现的

问题描述 C#初学者 代码看不懂,求大神,用了DBhelper之后两段代码是怎么实现的 //修改学院信息 private void btnupdate_Click(object sender, EventArgs e) { int index = dgvcollege.CurrentRow.Index; //取得选中行的索引 CollId = dgvcollege.Rows[index].Cells["CollegeId"].Value.ToString(); CollName = d

指针-@C语言大神,这程序谭浩强给的答案看不懂,求解释?

问题描述 @C语言大神,这程序谭浩强给的答案看不懂,求解释? //题目:输出字符串里数字团,以及个数.比如89cy129//就输出89,129,一共2个数.// 问题:后面一大段为啥要复制粘贴前面一大段的代码?//有什么用?求解释.而且运行也不对,求修改!跪谢! include int main() { char str[80],*pstr; int m,i=0,j=0,k=0,digit=0,ndigit=0,e10,a[80],*pa; pstr=&str[0];pa=&a[0]; g

ext js-Ext js添加javascript动作,看不懂,求解释。

问题描述 Ext js添加javascript动作,看不懂,求解释. screen.refresh('temp_extra_params;poll_interval=15;process;AUTO-GENERATE-REPORT;options=reportGEF020|47|REQGENERATE-XMLmessageRequest received.:lf:It may take up to 20 seconds to produce the VAF Request which will b

Java中的死锁,看不懂,请大虾帮帮。。。

问题描述 classZhangsan{publicvoidsay(){System.out.println("张三对李四说:"你给我画,我就把书给你"");}publicvoidget(){System.out.println("张三得到画了");}}classLisi{publicvoidsay(){System.out.println("李四对张三说:"你给我书,我就把画给你"");}publicvoi

html-AngularJS监听时间代码我看不懂,求大神解答

问题描述 AngularJS监听时间代码我看不懂,求大神解答 angular.module("ezstuff",[]) .directive("ezNamecardEditor",function(){ return { restrict : "E", template : " ", replace : true, link : function(scope,element,attrs){ //获得变量名称 var model