加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

HDU 1002 A + B Problem II

发布时间:2020-12-14 03:04:34 所属栏目:大数据 来源:网络整理
导读:A + B Problem II Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 212144????Accepted Submission(s): 40924 Problem Description I have a very simple problem for you. Given two integers

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 212144????Accepted Submission(s): 40924


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 = 2222222222222221110
?
题目大意:
? ? ? ? 给你n组很大的数,每组两个让你来求他们每组的和。

解题思路:
? ? ? ?用字符数组来存储两个大数,然后将它们从尾开始相加,在设置一个整型数组用来存储每位相加的和,同时还要注意进位。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char a[1005],b[1005];
int s[1005];
int mmax(int a,int b)
{
    if(a>b)
        return a;
    else
        return b;
}
int main()
{
    int n,i,j;
    int al,bl,t,k;
    scanf("%d",&n);
    for(k=1;k<=n;k++)
    {
        scanf("%s%s",a,b);
        al=strlen(a);//读取字符串长度
        bl=strlen(b);
        //printf("%d  %dn",al,bl);
        t=0;//记录进位
        for(i=al-1,j=bl-1;i>=0&&j>=0;i--,j--)
        {
            t=t+a[i]-'0'+b[j]-'0';
            if(t>=10)
            {
                if(al>bl)
                    s[i+1]=t%10;
                else
                    s[j+1]=t%10;
                t=1;
            }
            else
            {
                if(al>bl)
                    s[i+1]=t;
                else
                    s[j+1]=t;
                t=0;
            }
        }
        if(i<0)
        {
            while(j>=0)
            {
                s[j+1]=b[j]+t-'0';
                if(s[j+1]>=10)
                {
                    s[j+1]=s[j+1]%10;
                    t=1;
                }
                else
                    t=0;
                j--;
            }
            s[0]=t;
        }
        if(j<0)
        {
            while(i>=0)
            {
                s[i+1]=a[i]+t-'0';
                if(s[i+1]>=10)
                {
                    s[i+1]=s[i+1]%10;
                    t=1;
                }
                else
                    t=0;
                i--;
            }
            s[0]=t;
        }
         printf("Case %d:n",k);
         printf("%s + %s = ",b);
         if(s[0])
            printf("%d",s[0]);
         for(i=1;i<=mmax(al,bl);i++)
         {
             printf("%d",s[i]);
         }
         if(k==n)
            printf("n");
         else
            printf("nn");
    }
    //printf("%sn",a);
    //printf("%sn",b);
    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读