NYOJ103A+B Problem II

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103


纯模拟,没啥好说的 - -

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char s[1010];
char s1[1005];
int s2[1005];
int len_s;
int len_s1;

void Reverse(char s[],int len)

{
    for(int i = 0,j = len - 1;i < j && i < len / 2;++i,--j)
    {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }

}
int main()

{
    int cnt = 1;
    int _;
    scanf("%d",&_);
    while(_--)
    {
        memset(s2,0,sizeof(s2));
        scanf("%s%s",s,s1);
        len_s = strlen(s);
        len_s1 = strlen(s1);
        printf("Case %d:\n",cnt++);
        printf("%s + %s = ",s,s1);
        Reverse(s,len_s);
        Reverse(s1,len_s1);
        //printf("%s %s\n",s,s1);
        int c = 0;
        int i;
        for(i = 0;i < min(len_s,len_s1);++i)
        {
            s2[i] = (s[i] - '0' + s1[i] - '0' + c) % 10;
            //printf("%d %d %d %d\n",s[i] - '0',s1[i] - '0',s2[i],s[i] - '0' + s1[i] - '0' + c);
            c = (s[i] - '0'+ s1[i] - '0' + c) / 10;
        }
        while(i < len_s)
        {
            s2[i] = (s[i]  - '0' + c) % 10;
            c = (s[i] - '0' + c) / 10;
            i++;
        }

        while(i < len_s1)
        {
            s2[i] = (s1[i] - '0' + c) % 10;
            c = (s1[i] - '0' + c) / 10;
            i++;
        }
        while(c)
        {
            s2[i++] = c % 10;
            c /= 10;
        }
        for(int j = i - 1;j >= 0;--j)
            printf("%d",s2[j]);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(模拟,ACM,大数)