码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-10-14 23:35:58      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:

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 ≤ 107 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;
}

 

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

标签:

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/5962226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!