Big Number(log10求位数)

来源:互联网 发布:托管淘宝店铺 编辑:程序博客网 时间:2024/06/05 17:19

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
 

题意:

就是让你求n!有几位,如果普通的求法,会爆!!

思路:

运用log10求位数!!

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 15#define MAXN 100000005#define mod 7#define INF 0x3f3f3f3f#define exp 1e-6#define pi acos(-1.0)using namespace std;int main(){    //freopen("D:\\a.txt","r",stdin);   int n,i,t;   ios::sync_with_stdio(false);   double sum;   cin>>t;   while(t--)   {       sum=0;       cin>>n;       for(i=1;i<=n;i++)        sum=sum+log10(i);       cout<<(int)sum+1<<endl;   }    return 0;}

此题和寒假集训时做过的一个题一摸一样!!附上:

Description

元宵节到了,Snow也准备一掷千金买汤圆来庆祝元宵节。Snow非常慷慨,他将分享给你一定数量的汤圆,那么是多少个汤圆呢?答案是 n!n的阶乘)……嘻嘻别傻了,Snow要分享给你的是 n!的位数个汤圆,但前提是你得求出 n!有多少位哦。

Input

输入数据有多组(数据组数不超过 500),到EOF 结束。

每组数据输入 n (1 <= n <= 500)

Output

对于每组数据,输出一行,表示 n!的位数。

Sample Input

1

20

Sample Output

1

19

Hint

#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int i,x,j,a[9999]={0},b[9999]={0},len=0;
//定义数组存放大数,a为暂时存放数值
a[1]=b[1]=1; 

a[0]=b[0]=1;
for(i=1;i<=n;i++)
{
x=0; 

len+=4;//len为位数,因为n最大为500,所以len每次最大加4位
for(j=1;j<=len;j++)
{
b[j]=a[j]*i+x;
x=b[j]/10;
b[j]%=10;
}
while(b[len]==0&&len>1)
len--;
//删除前导0,使位数正好
for(j=1;j<=len;j++)
a[j]=b[j];
}
cout<<len<<endl;
}
return 0;
}

#include<iostream>

#include<cstring>

using namespace std;

int a[100000],n,i,y,xy[100000];

int main()

{

cin>>n;

a[0]=1;

a[1]=1;

for (y=1;y<=n;y++)

{

memset(xy,0,sizeof(xy));

    xy[0]=a[0];

    for (i=1;i<=a[0];i++)

    {

  xy[i]+=a[i]*y;

  xy[i+1]=xy[i]/10;

  xy[i]%=10;

}

    while (xy[xy[0]+1]>0)

{

xy[xy[0]+2]=xy[xy[0]+1]/10;

xy[xy[0]+1]%=10;

xy[0]++;

    }

    for (i=1;i<=xy[0];i++) a[i]=xy[i];

    a[0]=xy[0];

}

for (i=a[0];i>=1;i--) cout<<a[i];

cout<<endl;

cout<<a[0] ;

cout<<endl;

return 0;

}


f题考察高精度计算

#include<algorithm>

#include<string>

#include<cmath>

using#include<iostream>

 namespace std;

int main()

{

    int n,i,j,k;

    double sum;

    while(cin>>n)

    {

    for(i=1;i<=n ;i++)

    {

        sum=sum+log10(i);

    }

    cout<<(int)sum+1<<endl;

    }

    return 0;

}


原创粉丝点击