(Java)求n的階乘

2021-07-23 18:20:21 字數 704 閱讀 6146

當n整數值較小時,無須考慮n!是否會超出資料型別的範圍

public

static

long

getnfactorial(int n)

return n*getnfactorial1(n-1);

}

public

static

long getnfactorial1(int n)

long

sum=1l;

for(int i=1;i<=n;i++)

return

sum;

}

當n整數值較大時,n!會超出資料型別的範圍,使用陣列模擬大數的乘法

思路:主要在進製的考慮

1*2*3*4*5=120 在陣列中為021

陣列中 021 *6= 0 2 7

public

static

void

getnfactorial(int n) else

// 表示向新的位置進製

while (h > 0)

p = j - 1;

}for (i = p; i >= 1; i--)

}}

求N階乘的長度

給乙個數x,len log10 x 1就是x這個數的長度 當n的值不超過10 6時 那麼n的階乘長度 log10 1 2 3 4 n 1 log10 1 log10 2 log10 3 log10 4 log10 n 1 include includeint main 當n比較大時,再用上面這種方法...

求n的階乘(10000)

題意 如題目 思路 用乙個陣列,陣列的每一位存乙個8位數,兩個for迴圈,不斷從1開始累乘。include include include include using namespace std const int maxn 100000000 注意不能超過long long 的表示範圍 long ...

遞迴 求N!的階乘

用遞迴演算法,求n!的精確值 n以一般整數輸入,n 100 輸入乙個整數n 輸出n!1010 3628800顯然100資料規模的階乘是無法用longlong存下的,所以我選擇用陣列來模擬大數乘法,具體 實現是從社團學姐那裡學到的,這裡附上我的理解 include include include in...