和你一样爱上了雨水。滴滴答答落到我俩的心头。

424 - Integer Inquiry (AC)

上一篇 / 下一篇  2009-12-02 01:35:48 / 个人分类:UVA online judge

accepted
若使用java的bigInteger class能轻松的解决这题,但是为了训练programming的能力,我选择了用C++自己写。


#include<iostream>
#include<string>
#include<stack>

using namespace std;

int main(){

    stack<int> s1,s2,s3;
    string num1,num2,sum;
    int size;
    int carry=0;


    cin>>num1>>num2;

    for(int i=0;i<num1.length();i++){
        s1.push(num1[i]-48);
    }
    while(num2!="0"){
    for(int i=0;i<num2.length();i++){
        s2.push(num2[i]-48);
    }

    if(s1.size()<s2.size()){
        size=s1.size();
    }else{
        size=s2.size();
    }

    for(int k=0;k<size;k++){
        s3.push((carry+s1.top()+s2.top())%10);
        carry=(carry+s1.top()+s2.top())/10;
        s1.pop();
        s2.pop();
    }

    if(s1.empty()){
        for(int k=0;!s2.empty();k++){
            s3.push((carry+s2.top())%10);
            carry=(carry+s2.top())/10;
            s2.pop();
        }
    }else if(s2.empty()){
        for(int k=0;!s1.empty();k++){
            s3.push((carry+s1.top())%10);
            carry=(carry+s1.top())/10;
            s1.pop();
        }
    }

    if(carry!=0)
    s3.push(carry);

    carry=0;
    while(!s3.empty()){
        s1.push(s3.top());
        s3.pop();
    }
    cin>>num2;
    }

    while(!s1.empty()){
        s3.push(s1.top());
        s1.pop();
    }
    while(!s3.empty()){
        cout<<s3.top();
        s3.pop();
    }
    cout<<endl;
}


TAG: Inquiry Integer

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar