java中階乘
1. java 中階乘怎麼做
long result = 0;
long f = 1;
for(int i = 1; i <= 10; i++) {
f = f * i;
result += f;
}
System.out.println("result="+result);
10的階乘。多的不寫了。
2. java中階乘怎麼表示
沒有自帶方法表示階乘,必須自己通過循環來實現
3. java階乘問題
像你這個要設置一個最小值。用if,
當它階乘到了零的時候都應該停止運算。
4. 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)); } }其實方法還有很多,這里提供的也算是個框架形式。分享之
5. java中怎麼實現階乘
import java.util.Scanner;
public class Jiecheng {
public static void main(String[] args) {
System.out.print("請輸入你想要的階乘數:");
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
System.out.print("!" + a + "的結果是:" + jiecheng(a));
}
public static double jiecheng(int a) {
if (a <= 1) {
return a;
}
return jiecheng(a - 1) * a;
}
}
6. 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
7. java求階乘
/*
*使用遞歸求解n的階乘
*@param n--參數,被計算的階乘
*@return 返回n的階乘計算出來的結果
*/
public int doFactorial1(int n){
if(n<0){//傳入的n不合法
return -1;//返回-1,說明參數不合法
}
if(n==0){//0!=1
return 1;
}
else if(n==1){//退出遞歸的條件
return 1;
}else{//滿足進行遞歸的條件
return n*doFactorial1(n-1);
}
}
/*
*使用非遞歸求解n的階乘
*@param n--參數,被計算的階乘
*@return 返回n的階乘計算出來的結果
*/
public int doFactorial2(int n){
int result=1;//結果
if(n<0){//傳入的n不合法
return -1;//返回-1,說明參數不合法
}
if(n==0){//0!=1
return 1;
}
for(int i=1;i<=n;i++){//從1~n相乘
result*=i;
}
return result;//返回結果
}
8. 用java求階乘
求階乘用for就行,假設我們要對num求階乘,結果是result
int result = 1;
for (int i=1;i<=num;i++){
result *= i;
}
此時result的值即為num的階乘(僅正整數)
9. Java中關於階乘是怎麼回事。
階乘是數學上的一個概念,下面是對它的定義:
一個正整數的階乘(英語:factorial)是所有小於及等於該數的正整數的積,並且有0的階乘為1。自然數n的階乘寫作n!。
10. JAVA中怎麼表示階乘
1、首先在電腦打開eclipse軟體,創建Scanner對象。