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

时间:2023-03-08 16:47:01

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:log10(12345)=log10(1.2345*10^4)=4+log(1.2345);n的位数就是log10(n)+1;可以暴力。
2:斯特林公式:一个数的阶乘近似等于sqrt(2*PI*n)*(n/e)^n;
*/
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
int n,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
double ans=;
for(int i=;i<=m;i++)
ans+=log10(i);
printf("%d\n",(int)ans+);
}
return ;
} #include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double e=2.718281828459;
const double PI=3.14159265;
int main()
{
int n,m;
scanf("%d",&n);
while(n--)
{
double ans;
scanf("%d",&m);
if(m!=)
ans=0.5*log10(*PI*m)+m*log10(m)-m*log10(e)+;
else ans=1.0;
printf("%d\n",(int)ans);
}
return ;
}
//两个大数相加用字符串处理。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int main()
{
int t;
char s1[],s2[];
scanf("%d",&t);
for(int k=;k<=t;k++)
{
scanf("%s%s",s1,s2);
int ks1=strlen(s1);
int ks2=strlen(s2);
ks1--;ks2--;
int sav=,h=,a1,a2;
char s[];
while()
{
if(ks1<&&ks2<)
break;
if(ks1>=&&ks2>=)
{
a1=s1[ks1]-'';
a2=s2[ks2]-'';
}
if(ks1>=&&ks2<)
{
a1=s1[ks1]-'';
a2=;
}
if(ks1<&&ks2>=)
{
a1=;
a2=s2[ks2]-'';
}
ks1--;ks2--;
int tem=a1+a2+sav;
sav=tem/;
tem%=;
s[h++]=tem+'';
}
if(sav!=)
s[h++]=sav+'';
printf("Case %d:\n",k);
printf("%s + %s = ",s1,s2);
for(int i=h-;i>=;i--)
cout<<s[i];
printf("\n");
if(k!=t)
printf("\n");
}
return ;
}