HDU 1002 A + B Problem II
2013-07-07 19:29
363 查看
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 161989 Accepted Submission(s): 30809
[align=left]Problem Description[/align]
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 2
112233445566778899 998877665544332211
[align=left]Sample Output[/align]
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
这里要运用高精度来运算,用数组来存储数据,可以先定义字符数组,然后在进行字符与数字之间的转换,,最主要的是要考虑到进位的问题(关键)。下面是我的代码:
#include<iostream>
#include<cstring>
using namespace std;
const int MAXN=1000+10;
char str1[MAXN];
char str2[MAXN];
int str3[MAXN];
int str4[MAXN];
int str5[MAXN];
int main()
{
int T;
cin>>T;
int k=0;
while(T--)
{
int i=0;
static int j=1;
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(str3, 0, sizeof(str3));
memset(str4, 0, sizeof(str4));
cin>>str1;
cin>>str2;
int len1=strlen(str1);
for( i=0; i<len1; i++)
{
str3[i]=str1[len1-i-1]-'0';
}
int len2=strlen(str2);
for(i=0; i<len2; i++)
{
str4[i]=str2[len2-i-1]-'0';
}
int c=0;
for( i=0; i<MAXN; i++)
{
str5[i]=str3[i]+str4[i]+c;
c=str5[i]/10;
str5[i]%=10;
}
for(i=MAXN-1; i>=0 && str5[i]==0;i--);
cout<<"Case "<<j++<<":"<<endl;
cout<<str1<<" + "<<str2<<" = ";
if(i>=0)
{
for(; i>=0; i--)
{
cout<<str5[i];
}
}
cout<<endl;
if(T-1>=0)
{
cout<<endl;
}
}
return 0;
}
相关文章推荐
- hdu 1002 A+B problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- [hdu 1002] A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- hdu1002 A + B Problem II
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- [HDU1002] A + B Problem II
- hdu 1002 A + B Problem II
- HDU1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU_1002 A + B Problem II