1001 A + B Problem II(C语言 大数加法模板)

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

#include<stdio.h>
int len(char *a)//一个判断字符串长度的函数与strlen一样
{
    int i=0;
    while(*a++!='\0')
    {
        i++;
    }
    return i;
}
/***************************************************************/
int main()
{
    int i,j,la,lb,n,k;
    char a[1005],b[1005],t;//题目要求是1000这里注意
    scanf("%d",&n);//n组数据
	k=n;//仅仅为了输出格式
    while(n--)
    {
        t='0';//这个t在我的程序里很重要,往下看
        if(n==k-1)
            getchar();//防止数组得到不该得到的值
        for(i=0;;i++)
        {
            scanf("%c",&a[i]);
            if(a[i]==' ')//输入第一个数组时以空格结束
                break;
        }
        a[i]='\0';//!!!!!!注意!!!!!!加个'\0'否则会输出乱码
        for(j=0;;j++)
        {
            scanf("%c",&b[j]);//数组b同理
            if(b[j]=='\n')
                break;
        }
        b[j]='\0';//继续注意
/***************************************************************/
        la=len(a);//计算长度
        lb=len(b);
        if(la>=lb)//以长的那个数组为基础进行运算  
        {        
            printf("Case %d:\n%s + %s = ",k-n,a,b);//输出格式,不懂请看题目
            for(j=j-1,i=i-1;;j--,i--)//这里的for一定不能嵌套
            {
                if(b[j]<='9'&&b[j]>='0'&&j>=0)
				//这里有个非常重要的东西j>=0
				//这个条件是用来防止数组越界的,很重要。
                a[i]+=(b[j]-'0');//不懂请看asc码
                if(a[i]>'9'&&i>=0)//这个if语句是用来进位的
                {
                    a[i]-=10;
                    if((i-1)>=0)
                        a[i-1]++;
                    else
                        t++;
					//t是保证数组不越界的
					//像99+1=100这样的,99有两个元素
					//但是100有3个,t就是来保存那个1的
                }
                if(i==0)
                    break;
            }
            if(t<='9'&&t>'0')//注意控制输出格式
            {
                printf("%c",t);
                puts(a);
                if(n!=0)
                    printf("\n");
            }
            else
            {
                puts(a);
                if(n!=0)
                    printf("\n");
            }
        }
/***************************************************************/
        else
        {
            printf("Case %d:\n%s + %s = ",k-n,a,b);//下面同理,上面看懂了下面也懂了     
            for(j=j-1,i=i-1;;j--,i--)
            {
                if(a[i]<='9'&&a[i]>='0'&&i>=0)
                b[j]+=(a[i]-'0');
                if(b[j]>'9'&&j>=0)
                {
                    b[j]-=10;
                    if((j-1)>=0)
                        b[j-1]++;
                    else
                        t++;                    
                }
                if(j==0)
                    break;
            }
            if(t<='9'&&t>'0')
            {
                printf("%c",t);
                puts(b);
                if(n!=0)
                    printf("\n");
            }
            else
            {
                puts(b);
                if(n!=0)
                    printf("\n");
            }
        }
        
    }
    return 0;
}


阅读更多
换一批

没有更多推荐了,返回首页