写在前面
不得不说,UVA的格式控制是真的严格一些,在POJ上AC的代码,到了UVA就因为少了一个换行,WA了一发QAQ
思路
先将大数读入按照字符串格式,然后每一位累计加到对应的INT数组里,最后控制进位;
这里将数字的高位存到数组的高位,避免了移位对其各位的问题,直接就都是0为个位;
最后输出的时候,虽然M记录了数字长度,但是还是要控制一下前置无效0的问题就OK;
代码
#include <iostream>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 100000;
int a[maxn] = {0};
char ss[maxn] = {0};
int main()
{
string s;
int M = 0;
while(getline(cin,s) && s != "0")
{
int len = s.size();
M = max(M,len);
int j = 0;
for(int i = len - 1; i >= 0; i--)
{
if(isdigit(s[i]))
a[j++] += (s[i] - '0');
}
}
for(int i = 0; i < M; i++)
{
if(a[i] > 9)
{
a[i+1] += a[i] / 10;
a[i] = a[i] % 10;
M = max(i,M);
}
}
bool OK = false;
for(int i = M;i >= 0 ;i--)
{
if(a[i] == 0 && !OK)
{
continue;
}
cout << a[i];
OK = true;
}
cout << endl;
return 0;
}