Ⅰ java中怎么实现阶乘,如计算1~100的阶乘
使用BigInteger大容量运算类计算100的阶乘
一.一般算法(循环)
view plain to clipboardprint?
public class Test {
public static void main(String[] args) {
int result = 1;
for (int i = 1; i <= 100; i++) {
result *= i;
}
System.out.println(result);
}
}
public class Test {
public static void main(String[] args) {
int result = 1;
for (int i = 1; i <= 100; i++) {
result *= i;
}
System.out.println(result);
}
}
输出结果为0,因为int无法保存下100的阶乘的结果,100的阶乘的长度至少大于50位,也要大于long,double
二.使用BigInteger大容量运算类
view plain to clipboardprint?
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");//为result赋初始值,为1
for (int i = 1; i <= 100; i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);//调用自乘方法
}
System.out.println(result);//输出结果
System.out.println(String.valueOf(result).length());//输出长度
}
}
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
BigInteger result = new BigInteger("1");//为result赋初始值,为1
for (int i = 1; i <= 100; i++) {
BigInteger num = new BigInteger(String.valueOf(i));
result = result.multiply(num);//调用自乘方法
}
System.out.println(result);//输出结果
System.out.println(String.valueOf(result).length());//输出长度
}
}
计算结果为:000000000000000000
产度:158
Ⅱ Java编程:写出求n的阶乘的方法,并算出1到7的阶乘的和
Java编程:写出求n的阶乘的方法,并算出1到7的阶乘的和方法:
先编写求阶乘的方法,再通过for循环计算1到7的阶乘的和。
具体实现:
publicclassTest{
publicstaticvoidmain(String[]args){
intsum=0;//保存阶乘的和
for(inti=1;i<=7;i++)
sum+=factorial(i);
System.out.println(sum);
}
//求阶乘方法,传入一个整数,返回这个整数的阶乘
publicstaticintfactorial(intnum){
intresult=1;
for(inti=1;i<=num;i++){
result*=i;
}
returnresult;
}
}
Ⅲ 用java写的阶乘
public class DoWork extends Thread {
//author by sunvins 5.14
private int type;
private static Double result = 0d;
private static boolean isOver = false;
private static Double num = 0d;
private final Double maxN = 100d;
//最大记到的整数,我测的结果,最多取171就溢出了
public DoWork(int type) {
super();
this.type = type;
}
public static void main(String[] args) {
DoWork work1 = new DoWork(1);
DoWork work2 = new DoWork(2);
work1.start();
work2.start();
}
public void run() {
while (!isOver && type == 1) {// 注意:两个while不能合并
sumN(maxN);
}
while (num <= maxN && type == 2) {
// 读取结果
System.out.println("result=" + result + " n=" + num);
// work1停了,work2还要读最后一次的结果
if (num >= maxN)
break;
try {
sleep(10);// 小睡一会儿再读
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 算阶乘和
public void sumN(Double n) {
Double lastFactorial = 1d;
for (Double i = 1d; i <= n; i++) {
this.num = i;// 计数器,记住算到哪个数了
try {
sleep(5);// work1也要小小休息,不然太快了,work2都还来不及记,呵
} catch (InterruptedException e) {
e.printStackTrace();
}
// result += factorial(i);// 常规算法调用
lastFactorial *= i;
result += lastFactorial; //这是优化算法,只是不知道在这里有没意义
}
isOver = true;
}
// 算阶乘,这个是常规算法
// public Double factorial(Double n) {
// if (n < 1)
// return 1d;
// return n * factorial(n - 1);
// }
}
Ⅳ java计算n的阶乘
int n=8;
int p=n;
for(int i=n;i>1;i--){
p*=i;
}
print("%d\n",p);
Ⅳ java中阶乘怎么表示
java中可以用for循环来实现阶层。
代码如下:
public class Demo {
public static void main(String[] args) {
for(int i=1;i<5;i++){
int sum = 1;//定义一个用来存储阶层的值
for(int j=1;j<=i;j++){//实现阶层的循环
sum *= j;
}
System.out.println(i+"的阶层是:"+sum);
}
}
}
for循环中的变量从1开始,循环遍历到阶层本身的这个数,通过一个变量来记录上一个数的乘积即可。
Ⅵ 用java递归算法求一个数字的阶乘
用递归算法求一个数字的阶乘的程序如下:
public class JieCheng {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个整数:");
int n = in.nextInt();
System.out.println(n+"!="+f(n));
}
static long f(int n){
if (n==1) return 1;
else return n*f(n-1);
}
}
运行结果:
请输入一个整数:6
6!=720
Ⅶ java阶乘的算法是什么
public class Factorial { public static int factorial(int x) { if (x < 0) { throw new IllegalArgumentException(x must be=0); } int fact = 1; for (int i = 2; i <= x; i++) { fact *= i; } return fact; } public static void main(String args[]) { System.out.print(factorial(10)); }}这个是利用递归算法制成的。public class factorial2 { public static int factorial2(int x) { if (x < 0) { throw new IllegalArgumentException(x must be=0); } if (x <= 1) { return 1; } else return x * factorial2(x - 1); } public static void main(String args[]) { System.out.print(factorial2(17)); }}这个是数组添加的方法制成的,可以计算更大的阶乘。public class Factorial3 { static long[] table = new long[21]; static {table[0] = 1; } static int last = 0; public static long factorial(int x) throws IllegalArgumentException { if (x = table.length) { throw new IllegalArgumentException(Overflow; x is too large.); } if (x <= 0) { throw new IllegalArgumentException(x must be non-negative.); } while (last < x) { table[last + 1] = table[last] * (last + 1); last++; } return table[x]; } public static void main(String[] args) { System.out.print(factorial(4)); }}最后一个是利用BigInteger类制成的,这里可以用更大的更大的阶乘。import java.math.BigInteger;import java.util.*;public class Factorial4{ 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) { System.out.print(factorial(4)); } }其实方法还有很多,这里提供的也算是个框架形式。分享之
Ⅷ java输入一个数n,计算n的阶乘(5的阶乘=1*2*3*4*5)
1、首先要理解一下阶乘的公式:
n!=n*(n-1)*(n-2)*....*2*1,5!=5*4*3*2*1
#include//头文件stdio.h在新浪博客中无法显示加上就可以了
intmain()
{
intt=5,i=4;//要是求其他的数的阶乘的话,把t的值改为其他数,
//再把i改为(t-1)就行了
while(i>=1)
{
t=t*i;
i--;
}
printf("5的阶乘结果是:%d ",t);
return0;
}
2、运行结果如下: