uva-11111-栈

注意输入和输出的结果

-9 -7 -2 2 -3 -2 -1 1 2 3 7 9
-9 -7 -2 2 -3 -1 -2 2 1 3 7 9
-9 -7 -2 2 -3 -1 -2 3 2 1 7 9
-100 -50 -6 6 50 100
-100 -50 -6 6 45 100
-10 -5 -2 2 5 -4 -3 3 4 10
-9 -5 -2 2 5 -4 -3 3 4 9
-10 -5 -3 3 -1 1 5 -4 4 10

10
-10 10

-10

:-) Matrioshka!
:-( Try again.
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.
:-) Matrioshka!
:-( Try again.

#include <iostream>
#include <sstream>
#include<memory.h>
#include<stdio.h>
using namespace std;
const int N = 10000;
struct stack
{
    int a[N];
    int index;
    stack()
    {
        index = 0;
    }
    void push(int i)
    {
        a[index++] = i;
    }
    int pop()
    {
        int i = a[--index];
        return i;
    }
};
int main()
{
    int n;
    string str;
    while (getline(cin, str))
    {
        istringstream is(str);
        int hasInt = 0;
        stack s;
        memset(s.a, 0, sizeof(s.a));
        int error = 0;
        while (is >> n)
        {
            hasInt++;
            if (n < 0)
                s.push(n);
            else
            {
                int t = 0;
                while (s.index)
                {
                    int j = s.pop();
                    if (j > 0)
                        t += j;
                    else
                    {
                        if (j != n * -1)
                            error = 1;
                        break;
                    }
                }
                if (t < n && !error)
                    s.push(n);
                else
                    break;
            }
        }
        if (!hasInt)
        {
            //cout << ":-( Try again." << endl;
            continue;
        }
        if (hasInt == 1)
        {
            cout << ":-( Try again." << endl;
            continue;
        }
        if (error || s.index != 1)
            cout << ":-( Try again." << endl;
        else if (s.index == 1)
        {
            if (s.pop() > 0)
                cout << ":-) Matrioshka!" << endl;
            else
                cout << ":-( Try again." << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/7345754.html