A - A + B Problem II
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
SubmitStatus
Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
题目大意:2个小于1000位的数相加,求结果;
思路:用数组处理数据,避免数据溢出。使用逐位处理的方法实现加法,注意进位的情况。
代码如下:
#include
#include
using namespace std;
int main()
{
char a[1111],b[1111];
int c[1111],t;
scanf("%d",&t);
for(int h=1;h<=t;h++)
{
memset(c,0,sizeof(c));
cin>>a>>b;
int i,j,k=0,r=0,p;
for(i=strlen(a)-1,j=strlen(b)-1;i>=0&&j>=0;i--,j--)
{
p=a[i]-'0'+b[j]-'0'+r;
r=p/10;c[k++]=p%10;
}
while(i>=0)
{
p=(a[i]-'0')+r;
r=p/10;c[k++]=p%10;i--;
}
while(j>=0)
{
p=b[j]-'0'+r;
r=p/10;c[k++]=p%10;j--;
}
if(r!=0)
{
c[k++]=r;
}
printf("Case %d:\n",h);
cout<=0;i--)
{
cout<
B - N!
Time Limit:5000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
SubmitStatus
Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
Input
One N in one line, process to the end of file.
Output
For each N, output N! in one line.
Sample Input
123
Sample Output
126
题目大意:求n的阶乘,n小于10000;
思路:同上题,但阶乘对进位的要求高些,需要考虑高位进位问题。
代码如下:
#include
#define maxn 40000
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int count=1;
int a[maxn]={0};
a[0]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j9)
{
a[j+1]+=a[j]/10;
a[j]%=10;
if(j==count-1) //若最高位产生进位,则位数加 1
count++;
}
}
}
for(int i=count-1;i>=0;--i)
printf("%d",a[i]);
printf("\n");
}
return 0;
}
C - Hat's Fibonacci
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
SubmitStatus
Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646Note:No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
题目大意:斐波那契数列,但是数很大,题目限制结果不超过2005位数。
思路:用大数加法的方法处理数据,公式递推出答案。
代码如下:
#include
#include
#include
#include
#include
using namespace std;
string add(string s1,string s2)
{
string s="";
int i,j,x,y,k=0;
for(i=s1.size()-1,j=s2.size()-1;i>=0&&j>=0;i--,j--)
{
x=s1[i]-'0';y=s2[j]-'0';//每位转化为数字
s+=char((x+y+k)%10+'0');//相加转化为字符
k=(x+y+k)/10;//k为进位
}
while(i>=0)//串1比串2长,合并
{
x=s1[i]-'0';
s+=char((x+k)%10+'0');
k=(x+k)/10;//必须把k加上,可能2串合并时有进位
i--;
}
while(j>=0)//串2比串1长,合并
{
y=s2[j]-'0';
s+=char((y+k)%10+'0');
k=(y+k)/10;
j--;
}
if(k>0)
s+='1';//最高位的进位情况
reverse(s.begin(),s.end());//反转顺序
return s;
}
string s1,s2,s3,s4,s;
int main()
{
int n;
while(~scanf("%d",&n))
{
s1=s2=s3=s4="1";
if(n<=4)
{
printf("1\n");
continue;
}
for(int i=4;i