hdu1002(超超长数字相加)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/su_cicada/article/details/78503828

差点gg,用的是字符串数组来存数,加数,下面是原题
*保留了所有的注释
*没有进行代码缩减
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>
#include<string.h>
#include<iostream>
using namespace std;

void str(char s[1001],char d[1000],int &si,int &di)
{
    //int i;
    //si=di+1;
    /*for(i=xi;i<di;i++)
    {
        s[i+1]=x[xi];
    }*/
    si=di;
    di--;
    while(si)
    {
        //cout<<si<<"  "<<di<<"  "<<s<<"  "<<d<<endl;
        //printf("ssi  %d  ",s[si]);
        if(s[si]+d[di]>=10+48+48)
        {
            s[si-1]+=1;
            s[si]+=d[di]-48-10;
        }
        else
            s[si]+=d[di]-48;
        //printf("%d \n",s[si]);
        si--;
        di--;
    }
}
int main()
{
    int t,ai,bi,si,i,n;
    char a[1000]={0},b[1000]={0},s[1001]={0};
    while(scanf("%d",&t)==1)
    {
        n=1;
        while(t--)
        {
            scanf("%s%s",a,b);
            ai=strlen(a);
            bi=strlen(b);
            for(i=0;i<1001;i++)//(ai>bi?ai-bi:bi-ai)+1;i++)
                s[i]='0';
            if(ai>bi)
            {
                s[ai-bi+1]='\0';
                strcat(s,b);
                str(s,a,si,ai);
            }
            else//if(ai<bi)
            {
            //cout<<ai<<"  "<<bi<<"  "<<s<<endl;
                s[bi-ai+1]='\0';
                strcat(s,a);
                //cout<<s<<endl;
                str(s,b,si,bi);
            }
            if(s[0]==48)
            {
                s[0]=' ';
                printf("Case %d:\n%s + %s =%s\n",n++,a,b,s);
            }
            else
                printf("Case %d:\n%s + %s = %s\n",n++,a,b,s);
            if(t!=0)
                printf("\n");
        }
    }
}

/*int main()
{
    int t,ai,bi,si,n;
    char temp;
    char a[1000]={0},b[1000]={0},s[1001]={0};
    while(scanf("%d",&t)==1)
    {
        ai=bi=0;
        n=1;
        while(t--)
        {
            scanf("%s%s",a,b);
            ai=strlen(a)-1;
            bi=strlen(b)-1;
                if(bi>=0)
                    bi--;
                else
                    temp=b[bi];
                    b[bi]='0';
                if(ai>=0)
                    ai--;
                else
                    temp=a[ai];
                    a[ai]='0';
            printf("abc  %d %d %d\n",ai,bi,si);
                if(a[ai]+b[bi]-96>=10)
                {
                    s[si]+=a[ai]+b[bi]-58;
                    s[si-1]=1+'0';
                }
                else
                    s[si]+=a[ai]+b[bi]-48;
                si--;
                printf("!%d %d  %d\n",si,s[si+1],s[si]);
                //if((ai<0||bi<0))
                   // break;

            }
            if(strlen(a)>strlen(b))
                b[bi]=temp;
            else
                a[ai]=temp;
            printf("Case %d:\n%s +  %s = %s\n\n",n++,a,b,s);
            //s[si]=(a[ai]+b[bi]-96)>10?(a[ai]+b[bi]-58):(a[ai]+b[bi]-48);
        }
    }

*/
展开阅读全文

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