hdu1018+ cug1441N!的位数

hdu1018需要改格式

一个数n的位数为 log10(n) 向上取整;

则N!=1*2*……*n;两边同时取对数,log(N!) = log(1*2*……*N)。因为log(a*b) = loga + logb;

所以本题就变得很简单了

#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstdio>
 
using namespace std;
 
int main()
{
    int n;
    int k = 1;
    while(scanf("%d" , &n) != EOF)
    {
        double sum = 0;
        for(int i = 2 ; i <= n ; i ++ )
        {
            sum += (log10(i) );
        }
        sum += 1;
        int res = sum;
        printf("Case %dth is : %d.\n" , k++ , res);
    }
    return 0;
}


你可能感兴趣的:(hdu1018+ cug1441N!的位数)