常见的高精度问题,用string模拟解决,根据个人习惯偏好,只要能过,怎样写都行。
Run Time: 0sec
Run Memory: 312KB
Code length: 879Bytes
SubmitTime: 2012-01-28 21:44:57
// Problem#: 1159
// Submission#: 1201505
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
int i, j;
string r, s;
int sum, carry;
while ( cin >> N ) {
r = string( 101, '0' );
carry = 0;
while ( N-- ) {
cin >> s;
for ( i = s.size() - 1, j = 100; i >= 0; i--, j-- ) {
sum = ( r[ j ] - '0' ) + ( s[ i ] - '0' ) + carry;
carry = sum / 10;
r[ j ] = sum % 10 + '0';
}
while ( carry != 0 ) {
sum = ( r[ j ] - '0' ) + carry;
carry = sum / 10;
r[ j ] = sum % 10 + '0';
j--;
}
}
if ( r.find_first_not_of( '0' ) == string::npos )
cout << 0 << endl;
else
cout << r.substr( r.find_first_not_of( '0' ) ) << endl;
}
return 0;
}