码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1002 A + B Problem II 大数问题

时间:2018-04-06 19:38:26      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:contain   each   ber   cond   i++   contains   clu   题目   getch   

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 409136    Accepted Submission(s): 79277


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
 
思路:
采用字符数组储存两个加数,模拟小学的加法竖式计算
注意点:
1.俩个加数长度不等的时候,长度短的加数前面加0,0的个数为二者长度相减的绝对值
2.输出格式问题,只有输出最后一组数据的结果的时候,一个回车,其余都是两个回车
代码如下:
#include<bits/stdc++.h>
int main()
{
    int n;
    scanf("%d",&n);
    int y=1;
    while(y<=n)
    {
        char a[1005]= {0},b[1005]= {0},C[1005],A[1005],B[1005];
        getchar();
        scanf("%s %s",a,b);
        int l1=strlen(a);
        int l2=strlen(b);
        if(l1>l2)
        {
            int k=l1-l2;
            char d[k];
            for(int i=0; i<k; i++)
                d[i]=0;
            d[k]=\0;
            B[0]=\0;
            strcat(B,d);
            strcat(B,b);
            A[0]=\0;
            strcat(A,a);
        }
        else if(l1<l2)
        {
            int k=l2-l1;
            char d[k];
            for(int i=0; i<k; i++)
                d[i]=0;
            d[k]=\0;
            A[0]=\0;
            strcat(A,d);
            strcat(A,a);
            B[0]=\0;
            strcat(B,b);
        }
        else if(l2==l1)
        {
            A[0]=\0;
            B[0]=\0;
            strcat(A,a);
            strcat(B,b);
        }
        l1=strlen(A);
        l2=strlen(B);
        int k=0,cc=0;
        for(int i=l1-1,j=l2-1; i>=0&&j>=0; i--,j--)
        {
            int t=A[i]-0+B[j]-0+cc;
            if(t>=10)
            {
                cc=1;
                t=t-10;
            }
            else
            {
                cc=0;
            }
            C[k]=t+0;
            k++;
        }
        if(cc==1)
        {
            C[k]=1;
            C[k+1]=\0;
        }
        else
        {
            C[k]=\0;
        }
        int l3=strlen(C);
        printf("Case %d:\n",y);
        printf("%s + %s = ",a,b);
        for(int i=l3-1; i>=0; i--)
        {
            printf("%c",C[i]);
        }
        printf("\n");
        if(y!=n)
            printf("\n");
        y++;
    }
    return 0;
}

 

HDU1002 A + B Problem II 大数问题

标签:contain   each   ber   cond   i++   contains   clu   题目   getch   

原文地址:https://www.cnblogs.com/yinbiao/p/8728486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!