HDOJ 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

分析

大整数相加问题。另外输出的格式需要注意

#include <stdio.h>
int main()
{
    int n;
    int no=0;
    char a[1001],b[1001];
    scanf("%d",&n);
    while(n--)
    {
        if(no!=0)
            printf("\n");
        no++;
        scanf("%s %s",&a,&b);
        int alength=0;
        while(a[alength]!='\0')
            alength++;
        int blength=0;
        while(b[blength]!='\0')
            blength++;
        int length=alength;
        if(length>blength)
            length=blength;
        int ans[1001];
        for(int i=0;i<1001;i++)
            ans[i]=0;
        for(int i=0;i<length;i++)
        {
            int t1=a[alength-1-i]-'0';
            int t2=b[blength-1-i]-'0';
            if(t1+t2+ans[i]>9)
            {
                ans[i]=(t1+t2+ans[i])%10;
                ans[i+1]=1;
            }
            else
            {
                ans[i]=t1+t2+ans[i];
            }
        }
        if(length<alength)
        {
            for(int i=length;i<alength;i++)
            {
                if(a[alength-1-i]-'0'+ans[i]>9)
                {
                    ans[i]=(a[alength-1-i]-'0'+ans[i])%10;
                    ans[i+1]=1;
                }
                else
                {
                    ans[i]=a[alength-1-i]-'0'+ans[i];
                }
            }
        }
        if(length<blength)
        {
            for(int i=length;i<blength;i++)
            {
                if(b[blength-1-i]-'0'+ans[i]>9)
                {
                    ans[i]=(b[blength-1-i]-'0'+ans[i])%10;
                    ans[i+1]=1;
                }
                else
                {
                    ans[i]=b[blength-1-i]-'0'+ans[i];
                }
            }
        }
        printf("Case %d:\n",no);
        printf("%s + %s = ",a,b);
        int anslength=1000;
        while(ans[anslength]==0)
            anslength--;
        for(int i=anslength;i>=0;i--)
            printf("%d",ans[i]);
        printf("\n");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,788评论 0 33
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,998评论 0 23
  • 请不要再说什么, 我明白你的心情。 就让我们微笑着说声保重, 再挥手送走,昨日伤痛, 哪怕分别后,痛哭失声, 这一...
    项楠阅读 131评论 0 1
  • 宋胖子有句歌词:我也是个复杂的动物,嘴上一句带过心里却一直重复。我想有时候就是这样的吧,嘴上逞强,心里却一点也不放...
    1358阅读 4,030评论 0 2
  • 泛型泛型就是指参数化类型,用符号标记是实际类型的占位符,是从JDK1.5出现的新特性。泛型可分为:泛型类泛型方法泛...
    罗伯特胡萝卜皮阅读 850评论 0 9