Skip to content

Instantly share code, notes, and snippets.

@prehistoricpenguin
Last active December 14, 2015 11:29
Show Gist options
  • Save prehistoricpenguin/5079381 to your computer and use it in GitHub Desktop.
Save prehistoricpenguin/5079381 to your computer and use it in GitHub Desktop.
/*
uva 424
author chm
*/
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <list>
#include <queue>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
vector<vector<char> > svec;
vector<char> ans;
ostream& operator << (ostream& os, const vector<char>& chvec)
{
vector<char>::const_iterator ibeg = chvec.begin(),
iend = chvec.end();
while (ibeg != iend)
os << *ibeg++;
os << endl;
return os;
}
void printans()
{
int idx = 199;
while (idx >= 0 && ans[idx] == '0')
--idx;
cerr << "idx: " << idx << endl;
if (idx == -1) /* all zero */
cout << 0 << endl;
else /* print nozero part */
{
do
{
cout << ans[idx] ;
--idx;
}
while (idx >= 0);
cout << endl;
}
}
int main()
{
#ifndef ONLINE_JUDGE
assert(freopen("in", "r", stdin) != NULL);
assert(freopen("out", "w", stdout) != NULL);
#endif
svec.resize(200);
int i = 0;
ans = vector<char>(200, '0');
string line;
while ( cin >> line && line != "0")
{
svec[i] = vector<char> (200, '0');
/* copy the reverse string */
copy(line.rbegin(), line.rend(), svec[i].begin());
cerr << svec[i] << endl;
/* add the input line to sum */
int cry = 0,
tsum = 0;
for (int j = 0; j < 200; ++j)
{
tsum = ans[j] - '0' + cry + svec[i][j] - '0';
ans[j] = tsum % 10 + '0';
cry = tsum / 10;
}
++i;
}
cerr << ans;
printans();
#ifndef ONLINE_JUDGE
fclose(stdout);
assert(freopen("/dev/tty", "w", stdout) != NULL);
system("diff standard out");
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment