HDU ACM 1002 A + B Problem II

A + B Problem II

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
 
Author
Ignatius.L
 
感悟:
     这道题目,我做了好多次,也错了好多次,这是一道大数相加的题目,其实也不是太难了,只要你有耐性,有了思路很快便可以把代码打出来。
思路:
    每个加数,作为一个字符串处理,然后,对应的按照加法对应的法则,从每个大数的个位开始加起,然后,满十进一。不过处理的过程中要注意考虑到,每个加数的位数可能是不一样的,这时候就要单独处理了。具体解释在下面的代码中:
  (C语言版)
#include<stdio.h>
#include<string.h>  //字符串处理要包含的头文件。
#define M 1008  //定义每个加数的位数。
int main()
{
    int t,i,j,lenf,lens,flag,x,r,k,l;
    char first[M],second[M],result[M];
    scanf("%d",&t);  //测试数据的个数。
    k=1;l=t;   //K控制输出中的第几个case。
    while(t--)
    {
        scanf("%s%s",first,second);
        lenf=strlen(first)-1;
        lens=strlen(second)-1;  //lenf,lens分别代表第一个加数和第二个加数的长度。
        memset(result,0,sizeof(result));  //对result进行清零。
        x=flag=0;
        while(lenf>=0&&lens>=0)
        {
            r=first[lenf--]-'0'+second[lens--]-'0'+flag;  //flag是标志变量,当flag为1时,表示上一次两个大数某位相加大于9,进1。
            if(r>9)
            {
                r=r-10;
                result[x++]=r+'0';
                flag=1;
            }
            else
            {
                result[x++]=r+'0';
                flag=0;
            }
        }
        while(lenf>=0)  //L1
        {
            r=first[lenf--]-'0'+flag;
            if(r>9)
            {
                r=r-10;
                result[x++]=r+'0';
                flag=1;
            }
            else
            {
                result[x++]=r+'0';
                flag=0;
            }
        }
        while(lens>=0)  //L2
        {
            r=second[lens--]-'0'+flag;
            if(r>9)
            {
                r=r-10;
                result[x++]=r+'0';
                flag=1;
            }
            else
            {
                result[x++]=r+'0';
                flag=0;
            }
        }        //L1和L2是为了让某一个加数多余的位数可以加进来。
        if(flag)result[x++]=1+'0';
        printf("Case %d:\n",k++);
        printf("%s + %s = ",first,second);
        while(x--)printf("%c",result[x]);
        printf("\n");
        if(k-1!=l)
        printf("\n");
    }
    return 0;
}
注释:上述代码并非是作者原创,也参考了不少人的代码。大家一起探讨,共同进步!
 
                                                                                     (Never Give Up !)
 
 
就算天再高,那又怎样,踮起脚尖,就可以更靠近阳光!
原文地址:https://www.cnblogs.com/dreamapple/p/3104192.html