80 大整数相加
作者: xxx时间限制: 1S章节: 字符串
问题描述 :
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
输入说明 :
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.
输出说明 :
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.
输入范例 :
2
1 2
112233445566778899 998877665544332211
输出范例 :
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
代码实现一
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/*
思路:
1、将两个字符串,按末端对其方式,然后短的字符串缺位补0
2、两个字符串逆序
3、从遍历两个字符串,两个字符串对应位置逐一相加,如果>=10,则进一位
4、两个末尾和
1)>=10,则末端增加一位
2)<10,不做任何操作
5、逆序遍历longStr
*/
int main(){
int i,j,k,r,t,n,tempLen,lenShort,lenLong,flag;
char shortStr[1001],longStr[1001],tempStr[1001];
scanf("%d",&n);
for(i=1;i<=n;i++){
//两组输出数据中间隔一行
if(i>1){
printf("\n");
}
scanf("%s %s",shortStr,longStr);
//先输出
printf("Case %d:\n",i);
printf("%s + %s = ",shortStr,longStr);
lenShort=strlen(shortStr);
lenLong=strlen(longStr);
//统一转换成shortStr为短串,longStr为长串
if(lenShort>lenLong){
strcpy(tempStr,shortStr);
strcpy(shortStr,longStr);
strcpy(longStr,tempStr);
tempLen=lenShort;
lenShort=lenLong;
lenLong=tempLen;
}
//字符串逆序
strrev(shortStr);
strrev(longStr);
//短串前面补0
for(j=lenShort;lenShort<lenLong&&j<=lenLong;j++){
if(j==lenLong){
shortStr[j]='\0';
}else{
shortStr[j]='0';
}
}
//shortStr,longStr对应位置字符相加
flag=0;
for(k=0;k<lenLong;k++){
t=shortStr[k]-'0'+longStr[k]-'0';
if(flag==1){
t++;
}
if(t>=10){
t%=10;
//longStr[k]=t+'0';
flag=1;
}
longStr[k]=t+'0';
}
//遍历完毕,判断最后一次的和是否>=10;
if(flag==1&&k==lenLong){
longStr[lenLong]='1';
longStr[lenLong+1]='\0';
}
//逆序输出
for(r=lenLong;r>=0;r--){
if(r==0){
printf("%c\n",longStr[r]);
}else{
printf("%c",longStr[r]);
}
}
}
return 0;
}
代码实现二
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
int n,i,j,lenA,lenB,flag,t,len,tempT;
char shortStr[1001],longStr[1001],temp[1001],tempShort[1001],tempLong[1001];
scanf("%d",&n);
for(j=1;j<=n;j++){
if(j>1){
printf("\n");
}
scanf("%s %s",shortStr,longStr);
strcpy(tempShort,shortStr);
strcpy(tempLong,longStr);
strrev(shortStr);
strrev(longStr);
lenA=strlen(shortStr);
lenB=strlen(longStr);
//转换,不论输入的两个数字串谁大谁小,
//统一转换成shortStr小,longStr大
if(lenA>lenB){
strcpy(temp,shortStr);
strcpy(shortStr,longStr);
strcpy(longStr,temp);
len=lenB;
lenB=lenA;
lenA=len;
}
//遍历短的数字串
flag=0;
for(i=0;i<lenA;i++){
t=shortStr[i]+longStr[i]-'0'-'0';
if(flag){
t++;
}
if(t>=10){
longStr[i]=t%10+'0';
flag=1;
}else{
flag=0;
longStr[i]=t+'0';
}
}
//短的遍历完毕,并且shortStr[lenA-1]+longStr[lenA-1]>=10,需要高位进1
if(flag&&i==lenA){
t%=10;
//如果两串数字相同,并且两个数字串最高位相加>=10;则只需进一位
if(lenB==lenA){
//高位增加一位
longStr[i]=t+'0';
i++;
longStr[i]='\0';
}else{
//两串数字串长度不同,lenB>lenA,则从longStr的lenA位置开始循环
tempT=t;
while(i>=lenA&&i<lenB){
t=tempT+longStr[i]-'0';
if(t>=10){
longStr[i]=t%10+'0';
i++;
tempT%=10;
}else{
longStr[i]=t+'0';
break;
}
}
//如123+99987这种情况,较长的数字串遍历完毕,并且高位最后一个数字>=10,则数字串高位再加一位
if(i==lenB){
longStr[i]=tempT+'0';
i++;
longStr[i]='\0';
}
}
}
if(i>=lenB){
lenB=i;
}
printf("Case %d:\n",j);
printf("%s + %s = ",tempShort,tempLong);
//逆序遍历longStr
for(i=lenB-1;i>=0;i--){
if(i==0){
printf("%c\n",longStr[i]);
}else{
printf("%c",longStr[i]);
}
}
}
return 0;
}