計算乙個數階乘的位數

2021-07-27 14:20:19 字數 512 閱讀 2501

給出乙個數n,求出n!的位數。

暴力法肯定是不行的,階乘是個增長速度 很快的函式,10!已經有7位了。

更直接的方法是log10(n!) --以10為底n!的對數。 因為求位數就是要每次除以10 的,取對數的意義就是10的幾次方才能到n!,也就是求了n!有幾位。

那麼問題就轉化成求log10(n!)了

一種方法是換底公式然後求和log10(n!) = log2(1*2*3*..*n)/log2(10)分子拆開求和

這裡寫**片#include

#include

#include

using

namespace

std;

int main()

}

另外好像有個公式

拆開來算就可以了

hdu 1018 計算乙個數階乘的位數

input input consists of several lines of integer numbers.the first line contains an integer n,which is the number of cases to be tested,followed by n ...

統計乙個數的階乘後面0的個數

given an integer n,return the number of trailing zeroes in n note your solution should be in logarithmic time complexity.要求對數時間。首先分析,只有2 5 10才會產生乙個0,n...

求乙個數的階乘中0的個數

連續幾天產出為負,再不寫點東西沒法交代了。public class the number of 0 in factorialn 階乘中有多少0,如果n k 10 m k是乙個不能被10整除的數,那麼m有多少個就有多少個0 n!進行質因數分解n!2 x 3 y 5 z 由於只有2 5 10,所以可以看...