*寒假水96——A + B Problem II 【大数相加】

 

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. 

InputThe 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. 
OutputFor 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

 

#include<iostream>
#include<stdio.h>  
#include<string.h>  
#define max(x,y)  ( x>y?x:y )  
#define n 1010  

using namespace std;

int main()  
{    
    char a1[n]={0};  
    char b1[n]={0};  
    int a2[n]={0}, b2[n]={0};  
    int num ,n1,n2,j,xin,k,i,m1,m2;  
    cin>>num;  
    for(i = 1; i <=num; i++){  
        scanf("%s %s",a1,b1);  
        n1 = strlen(a1);  
        n2 = strlen(b1);  
        for(j = 0 ; j < n1; j++){  
            if(a1[j] != '0'&& a1[j] != NULL){  
                m1 = j;  
                break;  
            }else if(a1[j] == '0' && j == n1-1){  
                m1 = 0;  
                n1 = 1;  
                break;  
            }  
        }  
        for(j = 0 ; j < n2; j++){  
            if(b1[j] != '0' && b1[j] != NULL){  
                m2 = j;  
                break;  
            }else if(b1[j] == '0' && j == n2-1){  
                m2 = 0;  
                n2 = 1;  
                break;  
            }  
        }  
        for(j = n1-1,k = 0;j >=m1 ;j--,k++){  
            a2[k] = a1[j] - '0';  
        }  
          
        for(j = n2-1,k = 0;j >=m2 ;j--,k++){  
            b2[k] = b1[j] - '0';  
        }  
        xin = max(n1-m1,n2-m2);  
        int c2[n] = {0};  
        for(j = 0; j < xin ; j++){  
            c2[j] = a2[j] + b2[j] + c2[j];  
            c2[j+1] = c2[j] / 10;  
            c2[j] = c2[j] % 10;  
        }  
        if(c2[xin] == 0) xin--;  
        printf("Case %d:\n",i);  
        for(j = m1; j<n1;j++){  
            printf("%c",a1[j]);  
        }  
        printf(" + ");  
        for(j = m2; j<n2;j++){  
            printf("%c",b1[j]);  
        }  
        printf(" = ");  
        for(j = xin; j>= 0; j--) printf("%d",c2[j]);  
        if(i != num) {  
            printf("\n\n");  
        }else{  
            printf("\n");  
        }  
        for(j = 0; j<=xin; j++){  
            a2[j] = b2[j] = 0;  
        }  
    }  
    return 0;  
}  

题解:字符串形式读入两个加数,去掉没用的0,然后按位相加。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值