<untitled> (C++)

Ревизии: current

text/plain
text/html
source
Old rev.:
#include <iostream>
#include <stack>

using namespace std;

void No(){
    cout << ":-( Try again." << endl;
    exit(0);
}

int main()
{
    freopen("matriosh.in","r",stdin);
    freopen("matriosh.out","w",stdout);

    int c, inf = 1000*1000*1000;
    stack<int> val, sum;

    val.push(inf);
    sum.push(0);

    while (cin >> c && c != 0){
        if (c > 0){
            if (sum.empty() || val.empty() || val.top() != c || sum.top() >= val.top())
                No();

            val.pop();
            sum.pop();
        } else {
            int s;
            c = abs(c);
            if (!sum.empty()){
                s = sum.top();

                sum.pop();

                sum.push(s + c);
            }
            val.push(c);
            sum.push(0);
        }
    }

    if (val.empty() || val.top() != inf)
        No();

    cout << ":-) Matrioshka!" << endl;

    return 0;
}

 

Комментарии:

Нет