HDU 1018 大数(求N!的位数/相加)

本文介绍了一种计算大整数阶乘位数的方法,通过使用log10函数和斯特林公式实现。提供了两种算法的具体实现代码,并展示了如何处理大数相加的问题。

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35382    Accepted Submission(s): 16888


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
 题意:
求N!的位数。
代码:
 1 /*
 2 1:log10(12345)=log10(1.2345*10^4)=4+log(1.2345);n的位数就是log10(n)+1;可以暴力。
 3 2:斯特林公式:一个数的阶乘近似等于sqrt(2*PI*n)*(n/e)^n;
 4 */
 5 #include<iostream>
 6 #include<cmath>
 7 #include<cstdio>
 8 using namespace std;
 9 int main()
10 {
11     int n,m;
12     scanf("%d",&n);
13     while(n--)
14     {
15         scanf("%d",&m);
16         double ans=0;
17         for(int i=2;i<=m;i++)
18         ans+=log10(i);
19         printf("%d\n",(int)ans+1);
20     }
21     return 0;
22 }
23 
24 #include<iostream>
25 #include<cmath>
26 #include<cstdio>
27 using namespace std;
28 const double e=2.718281828459;
29 const double PI=3.14159265;
30 int main()
31 {
32     int n,m;
33     scanf("%d",&n);
34     while(n--)
35     {
36         double ans;
37         scanf("%d",&m);
38         if(m!=1)
39         ans=0.5*log10(2*PI*m)+m*log10(m)-m*log10(e)+1;
40         else ans=1.0;
41         printf("%d\n",(int)ans);
42     }
43     return 0;
44 }

 

//两个大数相加用字符串处理。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    int t;
    char s1[1005],s2[1005];
    scanf("%d",&t);
    for(int k=1;k<=t;k++)
    {
        scanf("%s%s",s1,s2);
        int ks1=strlen(s1);
        int ks2=strlen(s2);
        ks1--;ks2--;
        int sav=0,h=0,a1,a2;
        char s[1005];
        while(1)
        {
            if(ks1<0&&ks2<0)
            break;
            if(ks1>=0&&ks2>=0)
            {
                a1=s1[ks1]-'0';
                a2=s2[ks2]-'0';
            }
            if(ks1>=0&&ks2<0)
            {
                a1=s1[ks1]-'0';
                a2=0;
            }
            if(ks1<0&&ks2>=0)
            {
                a1=0;
                a2=s2[ks2]-'0';
            }
            ks1--;ks2--;
            int tem=a1+a2+sav;
            sav=tem/10;
            tem%=10;
            s[h++]=tem+'0';
        }
        if(sav!=0)
        s[h++]=sav+'0';
        printf("Case %d:\n",k);
        printf("%s + %s = ",s1,s2);
        for(int i=h-1;i>=0;i--)
        cout<<s[i];
        printf("\n");
        if(k!=t)
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/5962226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值