網頁

2013年8月22日 星期四

c014. Primary Arithmetic、UVA 10035

#include<iostream>
#include<string>

using namespace std ;

int main(){
    string num1, num2 ;
    int cb, cs ;
    string fill ;
    while(cin >> num1 >> num2){
        if(num1=="0" && num2=="0") break ;

        if(num1.length() > num2.length()){
            fill.assign(num1.length()-num2.length(),'0') ;
            num2 = fill+num2 ;
        }
        else{
            fill.assign(num2.length()-num1.length(),'0') ;
            num1 = fill+num1 ;
        }

        string::reverse_iterator rit1=num1.rbegin() ;
        string::reverse_iterator rit2=num2.rbegin() ;
        cb = 0, cs = 0;
        for(; rit1!=num1.rend() ; ++rit1, ++rit2){
            if(*rit1-'0'+*rit2-'0'+cb >= 10) cb=1, cs++ ;
            else cb=0;
        }
            if(cs>1)
            cout << cs << " carry operations." << endl ;
        else if(cs==1)
            cout << "1 carry operation." << endl ;
        else
            cout << "No carry operation." << endl ;
    }
    return 0 ;
}

沒有留言:

張貼留言