高精度A+B

#include<stdio.h>
#include<string.h> 
int main()
{
    int lenth1,lenth2,n,i,j,k,s;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        char a[1005]={0},b[1005]={0},c[1005]={0};
        int x[1005]={0},y[1005]={0},z[1005]={0};
        scanf("%s %s",a,b);
        lenth1=strlen(a);
        lenth2=strlen(b);
        k=(lenth1>lenth2)?lenth1:lenth2;
        for(j=0;j<lenth1;j++)
        {
            x[j]=a[lenth1-j-1]-'0';
        }
        for(j=0;j<lenth2;j++)
        {
            y[j]=b[lenth2-j-1]-'0';
        }
        for(j=0;j<=k;j++)
        {
            z[j]=z[j]+x[j]+y[j];
            if(z[j]>9)
            {
                z[j+1]++;
                z[j]=z[j]%10;
            }
        }
        j=k;
        while(z[j]==0)
        j--;
        if(j<0)
        {
            printf("Case %d:\n",i+1);
            printf("%s + %s = 0\n",a,b);
            if(i<n-1)
            printf("\n");
        }
        else
        {
            for(s=0;j>=0;j--,s++)
            c[s]=z[j]+'0';
            printf("Case %d:\n",i+1);
            printf("%s + %s = %s\n",a,b,c);
            if(i<n-1)
            printf("\n");
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/Leozi/p/10835173.html
今日推荐