Ⅰ 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、運行結果如下: