import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.math.BigInteger;
/**
* This program displays factorials as the user enters values interactively
*/
public class FactQuoter {
public static void main(String[] args) throws IOException {
// This is how we set things up to read lines of text from the user.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Loop forever
for (;;) {
// Display a prompt to the user
System.out.print("FactQuoter> ");
// Read a line from the user
String line = in.readLine();
// If we reach the end-of-file,
// or if the user types "quit", then quit
if ((line == null) || line.equals("quit"))
break;
// Try to parse the line, and compute and print the factorial
try {
int x = Integer.parseInt(line);
System.out.println(x + "! = " + Factorial4.factorial(x));
}
// If anything goes wrong, display a generic error message
catch (Exception e) {
System.out.println("Invalid Input");
}
}
}
}
/**
* This version of the program uses arbitrary precision integers, so it does not
* have an upper-bound on the values it can compute. It uses an ArrayList object
* to cache computed values instead of a fixed-size array. An ArrayList is like
* an array, but can grow to any size. The factorial() method is declared
* "synchronized" so that it can be safely used in multi-threaded programs. Look
* up java.math.BigInteger and java.util.ArrayList while studying this class.
* Prior to Java 1.2, use Vector instead of ArrayList
*/
class Factorial4 {
protected static ArrayList table = new ArrayList(); // create cache
static { // Initialize the first element of the cache with !0 = 1.
table.add(BigInteger.valueOf(1));
}
/** The factorial() method, using BigIntegers cached in a ArrayList */
public static synchronized BigInteger factorial(int x) {
if (x < 0)
throw new IllegalArgumentException("x must be non-negative.");
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);
}
/**
* A simple main() method that we can use as a standalone test program for
* our factorial() method.
*/
public static void main(String[] args) {
for (int i = 0; i <= 50; i++)
System.out.println(i + "! = " + factorial(i));
}
}
分享到:
相关推荐
python教程学习
tdac034.pdf
veclibm111111111111
nssm工具,进行2次解压后即可
视讯镜头优化,操作不走
【数据结构】的资源,包括书籍、在线课程和网站
北京大学-DeepSeek系列-提示词工程和落地场景.pdf.png
【财信国际经济研究院-2025研报】2025年2月CPI和PPI数据点评:通胀低于预期,内需亟待提振.pdf
【毕业设计】java-springboot+vue精品在线试题库系统源码(完整前后端+mysql+说明文档+LunW).zip
python安装-25.求 a 的 b 次方——有点不好意思哈.py
代码实现了自适应IMM(交互式多模型)算法,专注于对目标状态进行估计,并结合了匀速(CV)和匀加速(CA)运动模型。它使用自适应观测噪声估计和扩展卡尔曼滤波器(EKF)作为滤波主体,并在最后提供误差统计特性输出。
中产品工业物联网联网的基础功能,是企业开启数字化转型的入门之选。它能帮助企业快速搭建起基础的设备数据采集与监控体系,以较低成本实
北京大学DeepSeek系列-DeepSeek与AIGC应用.pdf.png
【毕业设计-java】springboot-vue基于多维分类的知识管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
## 一、数据介绍 本数据参考C刊《管理评论》佟岩(2024)老师的做法,使用各年度省级政府工作报告中“减碳”关键词的词频总数来测度地方政府对“减碳”的重视程度,“减碳”关键词包括二氧化碳、低碳、减排、节能、能耗、环境保护(环保)、生态、绿色。 本数据包含:原始数据、参考文献、代码do文件、最终结果。 ## 二、参考文献: 佟岩,李鑫,徐国铨.企业集团碳减排压力与债务分布-来自高耗能上市公司的经验证据[J].管理评论,2024,36(02):210-221.DOI:10.14120/j.cnki.cn11-5057/f.2024.02.014 ## 三、相关数据:城市、城市编码、年份、减碳重视程度词频。
python教程学习
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
python学习一些项目和资源
锐捷设备命令最全大合集
uniapp-machine-learning-from-scratch-05.rar