杭电acm1002算法分析和源码
时间:2025-07-11
时间:2025-07-11
杭电acm1002算法分析和源码
杭电acm1002算法分析和源码 冰之龙文本
专业人士进来
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 60478 Accepted Submission(s): 11083
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
杭电acm1002算法分析和源码
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110 这是的加法问题,32字节的数值,可用JAVA和c#里的32位整数。
这里我用c++写的代码,思路是 定义四组1000个字符的字符串,ABCD,然后比较AB二数的位数,取较小的位数k,循环
k次,用ASCII码48 '0' A的个位和B的个位相加减二个48加上v(初为0)的到t,把t和10取余加上48ASCII码给c[ ],t和10的商给v,也就是小学生的加法步骤。最后c的最高位赋值给最低位
注意:输出时的回车问题,每case换一行
#include<iostream>
using namespace std;
void main()
{int n=0,m=1;
cin>>n ;
while(n--){char c[1000],a[1000],b[1000];
int x=0,y=0,t=0,v=0,j,k,z=0;
cin>>a>>b;
while(a[x])x++;while(b[y])y++;
j=x<y?x:y;
for(k=0;k<j;k++,z++,x--,y--)
{t=a[x-1]-48+b[y-1]-48+v;c[z]=48+t%10;v=t/10;
}
if(x<y)
{j=y-x;
for(k=0;k<j;k++,z++,y--)
{t=b[y-1]-48+v;c[z]=48+t%10;v=t/10;}}else
{j=x-y;
for(k=0;k<j;k++,z++,x--)
{t=a[x-1]-48+v;c[z]=48+t%10;v=t/10;}}
if(v){c[z]=48+v;z++;c[z]=0;}else c[z]=0;
char d[1000];x=0;
while(c[x])x++;j=x;cout<<"Case "<<m<<":\n";m++;
for(k=0;k<j;k++,x--){d[k]=c[x-1];}d[k]=0;cout<<a<<" + "<<b<<" = "<<d<<'\n';if(n)cout<<'\n';
}}