1198_a+b

// 1198_a+b.cpp : 定義控制檯應用程序的入口點。
//題目1198:a+b
//時間限制:1 秒內存限制:32 兆特殊判題:否提交:8598解決:2968
//題目描述:
//實現一個加法器,使其能夠輸出a+b的值。
//輸入:
//輸入包括兩個數a和b,其中a和b的位數不超過1000位。
//輸出:
//可能有多組測試數據,對於每組數據,
//輸出a+b的值。
//樣例輸入:
//2 6
//10000000000000000000 10000000000000000000000000000000
//樣例輸出:
//8
//10000000000010000000000000000000
//來源:
//2010年華中科技大學計算機研究生機試真題

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "string"
#include "algorithm"
using namespace std;

int main()
{
    string a,b;
    char c[1010];
    while(cin>>a>>b){
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        int cn,i,j;
        cn = i = 0;
        while(i<a.length() && i<b.length()){
            c[i] = (a[i]-'0' + b[i]- '0' + cn)%10 + '0';
            cn = (a[i]-'0' + b[i] -'0'+ cn)/10;
            i++;
        }
        while(i<a.length()){
            c[i] = (a[i]-'0' + cn)%10 + '0';
            cn = (a[i]-'0' + cn)/10;
            i++;
        }
        while(i<b.length()){
            c[i] = (b[i]-'0' + cn)%10 + '0';
            cn = (b[i] -'0'+ cn)/10;
            i++;
        }
        if(cn==1){
            c[i] = 1+ '0';
            c[i+1] = '\0';
            reverse(c,c+i+1);
        }
        else{
            c[i] = '\0';
            reverse(c,c+i);
        }
        cout<<c<<endl;
    }
    return 0;
}

/*
reverse();
*/

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