UVA - 424 Integer Inquiry

来源:互联网 发布:seo查询 编辑:程序博客网 时间:2024/05/22 15:14

题目大意:大整数相加

解题思路:读入 char 类型字符串,倒序储存于 int 类型的数组中,相加大于 10 进位,可能比原来大一位,倒序输出。

#include<iostream> #include<cstdio>#include<string.h>using namespace std;int str[1000][1000];char s[1000];int ans[1000];int main() {    int tot = 0;    int max = 0;    while(scanf("%s", s) != EOF) {        if (strcmp(s,"0") == 0) break;        int l = strlen(s);        if (l > max) max = l;        int j = 0;        for (int i = l-1; i >= 0; i--)            str[tot][j++] = s[i] - '0';        tot++;    }    for (int i = 0; i < max ; i++)        for (int j = 0; j < tot; j++){            ans[i] += str[j][i];            if (ans[i] > 9) {                ans[i+1]++;                ans[i] -=  10;            }        }    int i;    if (ans[max] == 0) i = max - 1;    else i = max;    for (; i >= 0; i--)        printf("%d", ans[i]);    printf("\n");return 0; }
0 0
原创粉丝点击