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 ≤ 10
7 on each line.
Output
The output contains the number of digits in the factorial of the integers appearing in the input.
Sample Input
2 10 20
Sample Output
7 19
Source
Recommend
JGShining
答案:
#include <stdio.h>
#include <cmath>
int main()
{
int n, t, i;
double sum;
if (scanf("%d", &t) && t)
{
while (t--)
{
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; ++i)
{
sum += log10((double) i);
}
printf("%d/n", (long)ceil(sum));
}
}
return 0;
}
{
int n, t, i;
double sum;
if (scanf("%d", &t) && t)
{
while (t--)
{
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; ++i)
{
sum += log10((double) i);
}
printf("%d/n", (long)ceil(sum));
}
}
return 0;
}
下面是另一种算法,不过超时了,并且处理位数有限,但是可以求出相关的阶乘数:
#include <string>
int main()
{
long n, s, tmp;
long v;
char sum[100000000];
int j, w;
scanf("%d", &n);
while (n--)
{
scanf("%d", &v);
memset(sum, 0, sizeof(sum));
sum[0] = '1';
for (j = 2; j <= v; ++j)
{
s = 0;
for (w = 0; w < strlen(sum); ++w)
{
tmp = j*(sum[w]-'0')+s;
s = tmp/10;
sum[w] = tmp%10+'0';
}
if (s > 0)
{
do
{
sum[w++] = s%10+'0';
s = s/10;
}while (s > 0);
}
}
printf("%d/n", strlen(sum));
}
{
long n, s, tmp;
long v;
char sum[100000000];
int j, w;
scanf("%d", &n);
while (n--)
{
scanf("%d", &v);
memset(sum, 0, sizeof(sum));
sum[0] = '1';
for (j = 2; j <= v; ++j)
{
s = 0;
for (w = 0; w < strlen(sum); ++w)
{
tmp = j*(sum[w]-'0')+s;
s = tmp/10;
sum[w] = tmp%10+'0';
}
if (s > 0)
{
do
{
sum[w++] = s%10+'0';
s = s/10;
}while (s > 0);
}
}
printf("%d/n", strlen(sum));
}
return 0;
}
}