Monday, July 28, 2014

UVa - 424 - Integer Inquiry

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

int main(){
    int hand, temp;
    string num, ans;
    stack <char> numbers, sum, after_sum;
    bool flag = true;
    while(getline(cin, num)){
        int l = num.size();
        for(int i = 0; i < l; i++)numbers.push(num[i]);
        hand = 0;
        while(!sum.empty() && !numbers.empty()){
            temp = (sum.top()-48)+ (numbers.top()-48) + hand;
            if(temp > 9)hand = temp/10, temp %= 10;
            else hand = 0;
            after_sum.push(temp+48);
            sum.pop(), numbers.pop();
            if(sum.empty() && numbers.empty() && hand > 0)after_sum.push(hand+48);
        }
        while(!sum.empty()){
            temp = hand + (sum.top()-48);
            if(temp > 9)hand = temp/10, temp %= 10;
            else hand = 0;
            after_sum.push(temp+48);
            sum.pop();
            if(sum.empty() && hand > 0)after_sum.push(hand+48);
        }
        while(!numbers.empty()){
            temp = hand + (numbers.top()-48);
            if(temp > 9)hand = temp/10, temp %= 10;
            else hand = 0;
            after_sum.push(temp+48);
            numbers.pop();
            if(numbers.empty() && hand > 0)after_sum.push(hand+48);
        }
        if(flag)sum = numbers;
        if(num == "0"){
            while(!after_sum.empty())ans.push_back(after_sum.top()), after_sum.pop();
            cout << ans << endl;
            break;
        }
        while(!after_sum.empty())sum.push(after_sum.top()), after_sum.pop();
        flag = false;
        ans.clear();
    }
    return 0;
}

No comments:

Post a Comment