class test{ public static void main(String[] args){ fun f=new fun(); for(int i=0;i<=5;i++) System.out.print(f.fact1(i)+" "); System.out.println(); for(int i=0;i<=5;i++) System.out.print(f.fact2(i)+" "); System.out.println(); long [] p=f.fact3(5); for(int i=0;i<p.length;i++) System.out.print(p[i]+" "); System.out.println(); } } class fun{ long fact1(int n){ long result=1; if(n==0)return 0; for(int i=1;i<=n;i++) result=result*i; return result; } long fact2(int n){ if(n==0)return 0; if(n==1)return 1; else return n*fact2(n-1); } long [] fact3(int n){ long [] result=new long[n+1]; result[0]=0; result[1]=1; for(int i=2;i<=n;i++) result[i]=result[i-1]*i; return result; } }
② 關於JAVA階乘的演算法的解釋
//程序要能運行就必須用 public 修飾
public class jie
{
//程序入口
public static void main(String args[])
{
//定義變數sum用來保存階乘的結果的和
long sum = 0;
//定義變數i
int i;
//這是外循環,變數i的值從1向10遞增,
for(i=1;i<=10;i++)
{
//定義變數ji用來保存某個數的階乘結果
long ji = 1;
//定義變數J
int j;
//這是內循環,變數j將從1向i遞增
for(j=1;j<=i;j++)
{
//當j=1時 JI=JI*J=1*1=1;
//當J=2時 JI=JI*J=1*2=2;
//當J=3時 JI=JI*J=2*3=6
//當J=4時 JI=JI*J=6*4=24
//以此類推直到 J=I 時,循環結束...
ji *= j;//這行程序等效於: ji=ji*j;
}
//總和加上剛才某個數的階乘的結果
sum += ji;//這行程序等效於: sum=sum+ji;
}
//列印輸出結果
System.out.println("結果是:"+sum);
}
}
③ 用java求階乘
求階乘用for就行,假設我們要對num求階乘,結果是result
int result = 1;
for (int i=1;i<=num;i++){
result *= i;
}
此時result的值即為num的階乘(僅正整數)
④ java中階乘怎麼表示
沒有自帶方法表示階乘,必須自己通過循環來實現
⑤ 用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的階乘的方法,並算出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中怎麼實現階乘,如計算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,計算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、運行結果如下: