两个二进制数的加法,类似于高精度。
Run Time: 0sec
Run Memory: 312KB
Code Length: 806Bytes
Submit Time: 2011-06-12 12:38:10
// Problem#: 1201
// Submission#: 800058
// 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, N;
int i, j;
int add;
char key;
cin >> N;
for ( n = 1; n <= N; n++ ) {
string a, b;
cin >> a >> b;
string r = "";
for ( i = a.size() - 1, j = b.size() - 1, add = 0; i >= 0 || j >= 0 || add == 1; i--, j--, add /= 2 ) {
if ( i >= 0 )
add += a[ i ] - '0';
if ( j >= 0 )
add += b[ j ] - '0';
key = add % 2 + '0';
r = key + r;
}
for ( i = 0; i < r.size(); i++ ) {
if ( r[ i ] == '1' )
break;
}
r.erase( 0, i );
cout << n << " " << ( r.empty() ? "0": r ) << endl;
}
return 0;
}