Integer Inquiry

题目连接:http://bak2.vjudge.net/contest/135820#problem/C

题目大意: 大数相加用字符串,输入0时输入结束,如果只输入一个0,那么要输出0。。要特别注意特殊情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[105],s1[105];
int a1[105];
void add(char s1[],int a1[])
{
    int a2[105];
    for(int j=0,i=strlen(s1)-1;i>=0;i--)
    a2[j++]=s1[i]-'0';
    for(int i=0;i<strlen(s1);i++)
    {
        a1[i]=a1[i]+a2[i];
        if(a1[i]>=10)
        {
            a1[i]=a1[i]%10;
            a1[i+1]++;
        }
    }

}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
         int coun=0;
        memset(a1,0,sizeof(a1));
        while(scanf("%s",s)!=EOF)//此处不需要取址符,s就代表了地址,并且不能输入空格,但gets()可以
        {
            coun++;
            if(s[0]=='0') break;
            add(s,a1);

        }
        if(coun==1) printf("0");
        int g;
        for( g=104;g>=0;g--)
            if(a1[g]!=0) break;
        for(int j=g;j>=0;j--)
        printf("%d",a1[j]);
        printf("
");
       if(n!=0) printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Twsc/p/5950849.html