1154: A + B Problem (X)【附大整数相加过程详解】

题目描述

I have a very simple problem for you. Given two number 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 number, A and B. Notice that the number 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

提示

来源

付高磊

大整数相加详解教程欢迎参见本人另一篇博文:大整数专题之大整数相加【附过程详解+两种方法+测试数据+例题】

http://blog.csdn.net/coco56/article/details/79141528

这里我就直接贴代码了:

#include
#include
int main()
{
    char str1[1006],str2[1006],t,i;
    int num1[1006],num2[1006];
    scanf("%d",&t);
    int big_int(char str1[],char str2[],int num1[],int num2[]);
    for(i=1;i<=t;i++)
    {
        memset(num1,0,sizeof(num1));
        memset(num2,0,sizeof(num2));
        printf("Case %d:\n",i);
        scanf("%s %s",str1,str2);
        big_int(str1,str2,num1,num2);
        if(i-t)//C语言中只有0代表假,负数和正数均为真
        {
            putchar(10);//打印换行
            putchar(10);
        }
    }
}
int big_int(char str1[],char str2[],int num1[],int num2[])
{
    printf("%s + %s = ",str1,str2);
    int i,j,len1=strlen(str1),len2=strlen(str2),lenmax;
    lenmax=len1>=len2?len1:len2;
    for(i=len1-1,j=0;i>=0;i--,j++)
    {
        num1[j]=str1[i]-'0';
    }
    for(i=len2-1,j=0;i>=0;i--,j++)
    {
        num2[j]=str2[i]-'0';
    }
    for(i=0;i     {
        num1[i]+=num2[i];
        if(num1[i]>=10)
        {
            num1[i]-=10;
            num1[i+1]+=1;
        }
    }
    if(num1[lenmax])
    {
        printf("%d",num1[lenmax]);
    }
    for(i=lenmax-1;i>=0;i--)
    {
        printf("%d",num1[i]);
    }
}

你可能感兴趣的:(算法练习)