poj1423(排序)

http://poj.org/problem?id=1423

Big Number
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 22141Accepted: 7064

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 <= m <= 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

  题意:计算一个数的阶乘的结果有几位数。
一开始按照白书73页敲(当时就知道要TEL了),抱着侥幸的心理还是试了试,把组数开大。果然试了几次一直TEL果断放弃。
看了网上的题解发现是有公式的。
代码1:TLE
#include<iostream>
#include<cstring>
const int maxn =30000;
int f[maxn];
using namespace std;
int main()
{
  int i,j,n,t;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d",&n);
    memset(f,0,sizeof(f));
    f[0]=1;
    for(i=2;i<=n;i++)
    {
      int c=0;
      for(j=0;j<maxn;j++)
      {
        int s=f[j]*i+c;
        f[j]=s;
        c=s/10;
      }
    }
    for(j=maxn-1;j>=0;j--)
      if(f[j])
        break;
    printf("%d\n",j+1);
  }
  return 0;
}
 
 
代码2:AC

           
//log在c语言里面是自然对数
//可以用斯特林(Stirling)公式求解
//于是求n!的位数就是求log10((2*PI*n)^1/2*(n/e)^n)+1 即  1/2*log10(2*PI*n)+n*log10(n/e)+1
//const double PI=acos(-1.); 
//const double e=exp(1.);
//Stiring公式ap=log10(sqrt(2*PI*n))+n*log10(n/e)+1;
#include<iostream>
const double PI=3.141592653589;
const double e=2.718281828459;
#include<cmath>
using namespace std;
int main()
{
  int t;
  long n;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%ld",&n);
    double m=0.5*log10(2*PI*n)+n*(log10(n/e));//把加1放在下面要不然测试数据都有误差,放在下面的原因
    int g=(int)m+1.0;                                                //不知道             
    printf("%d\n",g);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值