hdu.1002 A + B Problem II

####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
问题来源:http://acm.hdu.edu.cn/showproblem.php?pid=1002

###翻译:
####问题描述
我有一个非常简单的问题。给定两个整数A和B,你的工作是计算A + B的和。

####输入
输入的第一行包含整数T(1 <= T <= 20),表示测试用例的数量。然后是T行,每行包含两个正整数,A和B.请注意,整数非常大,这意味着您不应该使用32位整数来处理它们。您可以假设每个整数的长度不超过1000。

####输出
对于每个测试用例,您应输出两行。第一行是“Case#:”,#表示测试用例的编号。第二行是方程“A + B = Sum”,Sum表示A + B的结果。注意方程中有一些空格。在两个测试用例之间输出一个空行。

####样本输入

2
1 2
112233445566778899 998877665544332211

####样本输出
Case 1:
1 + 2 = 3

Case 2:
12233445566778899 + 998877665544332211 = 1111111111111111110

####题意:
计算两个正整数的和,但是两个正整数可能很大。

####分析:
1.两个正整数可能很大,。
2.两个输出之间应该有一个空行,只有一个输出时候,是没有空行的
3.注意方程中有一些空格
4.注意还有编号的问题

####代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
    int n,t,x,y,z,i,j,k;
    int c[10000],d[10000];
    char a[10000],b[10000];
    scanf("%d",&n);
    t=0;//与输出换行有关
    k=n;
    while(n--)
    {
        t++;
        scanf("%s",a);//输入a
            x=strlen(a);//获取a的长度
        getchar();//吞行
        scanf("%s",b);//输入b
            y=strlen(b);//获取b的长度
            j=0;
        for(i=x-1;i>=0;i--)//将字符数组换成整数数组且数字反着存,下一步补足位数进行运算
        {
            c[j]=a[i]-'0';
            j++;
        }
        j=0;
        for(i=y-1;i>=0;i--)//将字符数组换成整数数组且数字反着存,下一步补足位数进行运算
        {
            d[j]=b[i]-'0';
            j++;
        }
        //判断a和b那个大
        if(x<y)//如果a<b
        {
            for(i=x;i<=y;i++)//补足位数
                c[i]=0;
            d[y]=0;//防止进位时候,溢出
            for(i=0;i<y;i++)//逐位运算,和10比较
            {
                if(c[i]+d[i]>=10)//和大于等于10,进位
                {
                    d[i]=(c[i]+d[i])%10;//得到进位后的该位数字
                    d[i+1]=1+d[i+1];//前一位值+1
                }
                else d[i]=c[i]+d[i];//得到和
            }
            printf("Case %d:\n%s + %s = ",t,a,b);
            if(d[y]!=0)//首位之和大于10产生的进位
            {
                for(j=y;j>=0;j--)
                    printf("%d",d[j]);
            }
            else//首位之和小于10,则前一位为0,不输出
            {
                for(j=y-1;j>=0;j--)
                    printf("%d",d[j]);
            }
            printf("\n");
        }
        else//如果a>=b
        {
           for(i=y;i<=x;i++)//补足位数
                d[i]=0;
            c[x]=0;//防止进位时候,溢出
            for(i=0;i<x;i++)//逐位运算,和10比较
            {
                if(c[i]+d[i]>=10)//和大于等于10,进位
                {
                    c[i]=(c[i]+d[i])%10;//得到进位后的该位数字
                    c[i+1]=1+c[i+1];//前一位值+1
                }
                else c[i]=c[i]+d[i];//得到和
            }
           printf("Case %d:\n%s + %s = ",t,a,b);
           if(c[x]!=0)//首位之和大于10产生的进位
            {
                for(j=x;j>=0;j--)
                    printf("%d",c[j]);
            }
            else//首位之和小于10,则前一位为0,不输出
            {
                for(j=x-1;j>=0;j--)
                    printf("%d",c[j]);
            }

        printf("\n");
        }
        if(t!=k)//控制输出空行,两个输出之前有个空行,一个输出无换行
            printf("\n");
    }
    return 0;
}

####运行结果:
这里写图片描述

总结:题意很简单,但是考虑的问题如分析得一样,少分析一点就会有问题,自己刚做的时候,也是错了几回才做出来的。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值