您的位置:首页 > 产品设计 > UI/UE

UVA - 424 Integer Inquiry

2016-07-06 15:58 609 查看
题目大意:大整数相加

解题思路:读入 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 高精度