Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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
在写这道题的时候发现这题的AC率极低。这题看似简单实际上坑还是挺多的。很多人匆忙下手没有考虑到很多细节部分,导致一直无法AC。
实际上这题应该属于高精度加法的模板题,写法还是有很多种的。我这里提供两种写法。
要注意的点主要有下面几个:
1.不能直接用int类型的变量来直接相加(题目已经说明了结果的长度在1000位以内连long long的范围都超出了)
2.每一组数据处理前要注意初始化变量值为0
3.在处理输入的数字和输出的数字的时候要注意前导0的问题
4.不要遗漏了最后一次的进位
5.最好将char类型的数组转移到int数组中处理会更省力(本文方法二)
6.注意输出格式的问题:
1)Case # #=1时之前不需要换行 #>=2时之前要加换行符
2)A + B = SUM 之后要换行
方法一:
#include <stdio.h>
#include <string.h>
void Reverse( char *,int );
int main(void)
{
char a[1003] = {0};
char b[1003] = {0};
char c[1003] = {0};
int n = 0;
int l = 0;
int la = 0;
int lb = 0;
int x = 0;
int num = 1;
int zeroid[2] = {0};
scanf("%d",&n);
for ( int i = 0 ;i < n ;i++ ) {
zeroid[0] = 0;
zeroid[1] = 0;
x = 0;
memset(a,0,1003);
memset(b,0,1003);
memset(c,0,1003);
scanf("%s %s",a,b);
while ( a[zeroid[0]] == '0' ) {
zeroid[0]++;
}
while ( b[zeroid[1]] == '0' ) {
zeroid[1]++;
}
la = strlen(a);
lb = strlen(b);
l = la > lb ? la:lb;
Reverse(a,la);
Reverse(b,lb);
for ( int j = 0 ;j < l ;j++ ) {
c[j] = a[j] + b[j] + x;
x = c[j] / 10;
c[j] %= 10;
}
c[l] = x;
if ( num != 1 ) printf("\n");
printf("Case %d:\n",num);
for ( int j = la-zeroid[0]-1 ;j >= 0 ;j-- ) {
printf("%d",(int)a[j]);
}
printf(" + ");
for ( int j = lb-zeroid[1]-1 ;j >= 0 ;j-- ) {
printf("%d",(int)b[j]);
}
printf(" = ");
zeroid[0] = l;
while ( c[zeroid[0]] == 0 ) {
zeroid[0]--;
}
for ( int j = zeroid[0] ;j >= 0 ;j-- ) {
printf("%d",(int)c[j]);
}
printf("\n");
num++;
}
}
void Reverse( char *a ,int length) {
char t[1003] = {0};
for ( int i = length-1 ;i >= 0 ;i-- ) {
t[length-i-1] = a[i] - 48;
}
for ( int i = 0 ;i < length ;i++ ) {
a[i] = t[i];
}
}
方法二:
#include <stdio.h>
#include <string.h>
#include <set>
using namespace std;
int lena,lenb,num,t,i,n,j,lenc;
int a[1020],b[1020],c[1020];
char s1[1020],s2[1020];
int main(){
scanf("%d",&n);
for(i=1;i<=n;++i){
scanf("%s%s",s1,s2); lena=strlen(s1); lenb=strlen(s2);
memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); num=0;
memset(c,0,sizeof(c));
lenc=max(lena,lenb);
for(j=0;j<=lena-1;++j){a[lena-j-1]=s1[j]-'0';}
for(j=0;j<=lenb-1;++j){b[lenb-j-1]=s2[j]-'0';}
for(j=0;j<=lenc;++j){
c[j]=a[j]+b[j]+num; num=0;
if(c[j]>=10){
c[j]-=10; num=1;
}
}
if(c[lenc]==0){--lenc;}
i==1?printf("Case %d:\n%s + %s = ",i,s1,s2):printf("\nCase %d:\n%s + %s = ",i,s1,s2);
for(j=lenc;j>=0;--j){printf("%d",c[j]);}
printf("\n");
}
return 0;
}