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

hdu 1002 A + B Problem II(大正整数相加)

时间:2015-08-14 22:49:02      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:acm

代码:

#include<cstdio>
#include<cstring>
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;

char s1[10000],s2[10000],s3[10000];

int main()
{
    int t;
    scanf("%d",&t);
    int tt=0;
    while(t--)
    {
        ++tt;
        if(tt!=1)
            printf("\n");
        scanf("%s%s",s1,s2);
        int len1,len2,len3;
        len1=strlen(s1);
        len2=strlen(s2);
        len3=Min(len1,len2);
        int k=0;
        int i=len1-1;
        int j=len2-1;
        int pos=0;
        while(len3--)
        {
            int temp=(s1[i--]-'0'+s2[j--]-'0'+k);
            s3[pos++]=(temp%10+'0');
            k=temp/10;
        }
        if(i==-1&&j==-1)
        {
            if(k!=0)
            s3[pos++]=k+'0';
        }
        else if(i==-1&&j!=-1)
        {

            while(j>=0)
            {
                int temp=s2[j--]-'0'+k;
                s3[pos++]=(temp%10+'0');
                k=temp/10;
            }
            if(k!=0)
                s3[pos++]=k+'0';
        }
        else
        {
            while(i>=0)
            {
                int temp=s1[i--]-'0'+k;
                s3[pos++]=(temp%10+'0');
                k=temp/10;
            }
            if(k!=0)
                s3[pos++]=k+'0';
        }
        printf("Case %d:\n",tt);
        printf("%s + %s = ",s1,s2);
        for(i=pos-1;i>=0;i--)
        {
            printf("%c",s3[i]);
        }
        printf("\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1002 A + B Problem II(大正整数相加)

标签:acm

原文地址:http://blog.csdn.net/xky1306102chenhong/article/details/47668057

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