Big Number

来源:互联网 发布:手机c语言表白代码 编辑:程序博客网 时间:2024/04/27 21:55
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
 

 

Source
Asia 2002, Dhaka (Bengal)
 

 

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;
}
下面是另一种算法,不过超时了,并且处理位数有限,但是可以求出相关的阶乘数:
#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));
 }
 return 0;
}
原创粉丝点击