注意输入和输出的结果
-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 = ;
struct stack
{
int a[N];
int index;
stack()
{
index = ;
}
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 = ;
stack s;
memset(s.a, , sizeof(s.a));
int error = ;
while (is >> n)
{
hasInt++;
if (n < )
s.push(n);
else
{
int t = ;
while (s.index)
{
int j = s.pop();
if (j > )
t += j;
else
{
if (j != n * -)
error = ;
break;
}
}
if (t < n && !error)
s.push(n);
else
break;
}
}
if (!hasInt)
{
//cout << ":-( Try again." << endl;
continue;
}
if (hasInt == )
{
cout << ":-( Try again." << endl;
continue;
}
if (error || s.index != )
cout << ":-( Try again." << endl;
else if (s.index == )
{
if (s.pop() > )
cout << ":-) Matrioshka!" << endl;
else
cout << ":-( Try again." << endl;
}
}
return ;
}