Big Number(log10求位数)

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
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;

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值