Sicily 1201. 01000001

兩個二進制數的加法,類似於高精度。

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;

}                                 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章