Big Number(HDU 1018)

来源:互联网 发布:绿地集团知乎 编辑:程序博客网 时间:2024/05/22 08:18

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26509    Accepted Submission(s): 12068


Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.
 

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 lines, one integer 1 ≤ n ≤ 107 on each line.
 

Output
The output contains the number of digits in the factorial of the integers appearing in the input.
 

Sample Input
21020
 

Sample Output
719
 


题意:

求n!有几位。


分析:

第一次看到这个题目时果断放弃了,今天又看到这个题目。由于做过类似的用log求大数问题的题,于是开始推导,结果没有想象的那么难。因为n!=10^lg(n!) ,又因为如果n!= 10^x ,那么 x+1 则代表n!有几位。故只需算出lg(n!)为多少,再加1则为n!的位数。因为lg(n!) = lg(n)+lg(n-1)+...+lg(2)+lg(1)。所以直接调用库函数log10循环运算即可。

#include<stdio.h>#include<math.h>int main(){    int T, n;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        double i, sum = 0;        for(i=2; i<=n; i+=1)            sum += log10(i);        printf("%d\n", int(sum)+1);    }    return 0;}

看到xiaoqiang同学也用类似的方法做出来了:直接看除了多少个10加1就行,xiaoqiang就是叼啊。

#include<stdio.h>#include<math.h>int main(){    int T, n;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        double sum = 1;        int num = 1;        for(int i=2; i<=n; i++)        {            sum *= i;            while(sum >= 10) sum /= 10, num++;        }        printf("%d\n", num);    }    return 0;}


0 0