1. 程式人生 > >51Nod-1058-N的階乘的長度

51Nod-1058-N的階乘的長度

輸入N求N的階乘的10進製表示的長度。例如6! = 720,長度為3。
Input
輸入N(1 <= N <= 10^6)
Output
輸出N的階乘的長度
Input示例
6
Output示例
3

遇見這樣的題,直接的思路就是用斯特林公式。
//n!=sqrt(2*PI*n)*(n/e)^n 這是求近似值 做相應的變換即可求得。

#include <stdio.h>
#include <math.h>
//斯特林公式n!=sqrt(2*PI*n)*(n/e)^n
#define PI 3.1415926

int main()
{
    int n, a;
    while
(~scanf("%d",&n)) { a = (int)((0.5 * log(2 * PI * n) + n * log(n) - n) / log(10)); printf("%d\n", a + 1); } return 0; }