POJ 1503 Integer Inquiry 水题

给出最多100个大数,每个数最多100位,求和。
模拟一下加法,注意进位就可以。

代码如下:

#include 
#include 
#include 
#include 
#include 
using namespace std;
char str[105];
char result[105];
int main()
{
    //freopen("input.txt", "r", stdin);
    for (int i = 0; i < 105; ++i)
        result[i] = '0';
    int carry, temp, pos;
    while (scanf("%s", str) != EOF)
    {
        int len = strlen(str);
        carry = 0;
        pos = 0;
        for (int i = len - 1; i >= 0; --i)
        {
            temp = str[i] - '0' + result[pos] - '0' + carry;
            result[pos++] = temp % 10 + '0';
            carry = temp / 10;
        }
        while (carry != 0)
        {
            temp = result[pos] - '0' + carry;
            result[pos++] = temp % 10 + '0';
            carry = temp / 10;
        }
    }
    pos = 0;
    for (int i = 104; i >= 0; --i)
    {
        if (result[i] != '0')
        {
            pos = i;
            break;
        }
    }
    for (int i = pos; i >= 0; --i)
        printf("%c", result[i]);
    printf("\n");
    //printf("end\n");
    //while (1);
    return 0;
}

你可能感兴趣的:(水题,POJ)