//4_7_23: Integer Inquiry 高精度整数累加 POJ1503 ZOJ1292
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int i,j,temp,plus,count,length;
char str[110][110];
int ans[210];
int cnt = 0;
int len = 0;
while(1)
{
cin.getline(str[cnt],110);
length = strlen(str[cnt]);
if(length == 1 && str[cnt][0] == '0') break;
i = 0;
while(str[cnt][i] == '0') i++;
if(i > 0) for(j = i;j <= length;j ++) str[cnt][j - i] = str[cnt][j];
length = strlen(str[cnt]);
if(length > len) len = length;
for(i = 0,j = length - 1;i < j;i ++,j --)
{
temp = str[cnt][i];
str[cnt][i] = str[cnt][j];
str[cnt][j] = temp;
}
cnt++;
}
for(i = 0;i < cnt;i ++)
{
length = strlen(str[i]);
if(length < len)
{
for(j = length;j < len;j ++) str[i][j] = '0';
str[i][len] = '\0';
}
}
memset(ans,0,sizeof(ans));
plus = 0;
for(j = 0;j < len;j ++)
{
for(i = 0;i < cnt;i ++) ans[j] += (int)(str[i][j] - '0');
temp = plus;
plus = (ans[j] + temp) / 10;
ans[j] = (ans[j] + temp) % 10;
}
count = len;
while(plus != 0)
{
ans[count++] = plus % 10;
plus = plus / 10;
}
for(i = count - 1;i >= 0;i --) printf("%d",ans[i]);
printf("\n");
return 0;
}
/*测试结果:通过POJ1503检测,ZOJ1292是不同格式的输入和输入,需要修改
123134
23498743293207
00002344398723232
9234
00234
0
25843142149041
请按任意键继续. . .
*/
POJ1503 ZOJ1292 Integer Inquiry
最新推荐文章于 2016-08-18 00:40:27 发布