原题代号:HDU 1002
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
原题描述:
Problem 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
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
题目大意:完成长度在1000以内的任意两数相加 (转换成字符串处理)
解法:
# include <stdio.h> # include <string.h> # include <stdlib.h> # include <iostream> # include <fstream> # include <vector> # include <queue> # include <stack> # include <map> # include <math.h> # include <algorithm> using namespace std; # define pi acos(-1.0) # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define For(i,n,a) for(int i=n; i>=a; --i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define Fo(i,n,a) for(int i=n; i>a ;--i) typedef long long LL; typedef unsigned long long ULL; char a[1000+5],b[1000+5],c[1000+5]; int main() { int t,sum=0; cin>>t; while(t--) { if(sum)cout<<endl; char str1[1000+5],str2[1000+5]; mem(a,'\0'); mem(b,'\0'); mem(c,'\0');//因为多组数据,所以清零 cin>>str1; for(int i=sizeof(a),j=strlen(str1)-1; j>=0; j--,i--) a[i]=str1[j]-48; cin>>str2; for(int i=sizeof(b),j=strlen(str2)-1; j>=0; j--,i--)//将字符串放在数组末尾,并且将字符转换成ASCLL码所对应的数字 b[i]=str2[j]-48; int num=0,i; for(i=sizeof(a); i>0; i--) { c[i]=a[i]+b[i]+num; num=c[i]/10; c[i]%=10; } printf("Case %d:\n%s + %s = ",++sum,str1,str2); for(i=0;i<=10005;i++)if(c[i])break; for(int j=i; j<=sizeof(c); j++) printf("%d",c[j]);//将大数表示出来 cout<<endl; } return 0; }