题目意思就是一排套娃,里面的必须小于外面的,(不能等于)
做法就是用栈模拟,我是匹配到一个正数,并且这是栈顶是正数是 ,然后一直找到找到栈顶是这个数的相反数为止,把正数加起来与这个数比;;
代码有点乱。。不太想动了,,,
#include<iostream>
#include<string>
#include<stack>
#include<stdio.h>
#include<string.h>
using namespace std;
int main () {
char ch;
long long num[100000];
int w = 1;
while (cin >> num[0]) {
if (num[0] > 0) {
cout<<":-( Try again."<<endl;
continue;
}
stack<long long > sta;
int i = 1;
int z = 0 ;
int f = 0 ;
bool find = true;
int count = 0;
bool b = true;
ch = getchar();
for( i = 1 ;;i++) {
cin >> num[i];
ch = getchar();
if (ch == '\n')
break;
}
for (int k = 0;k <= i;k++){
if(num[k] > 0)
z++;
else
f++;
}
if (z != f) {
cout<<":-( Try again."<<endl;
continue;
}
if ( i % 2 == 0) {
cout<<":-( Try again."<<endl;
continue;
}
for (int j = 0; j <= i ; j++) {
if (num[j] < 0) {
sta.push(num[j]);
continue;
}
if (num[j] > 0 && sta.top() < 0 && sta.top() == -num[j]) {
sta.push(num[j]);
continue;
}
if(num[j] > 0 && sta.top() < 0 && sta.top() != -num[j] ) {
cout<<":-( Try again."<<endl;
b = false;
break;
}
while (sta.top() != -num[j]) {
if(sta.top() > 0)
count += sta.top();
sta.pop();
if (sta.empty())
break;
}
if(sta.empty()) {
find = false;
}
sta.push(num[j]);
if (count >= num[j])
find = false;
count = 0;
}
if(b == false)
continue;
if(find == true)
cout <<":-) Matrioshka!"<<endl;
else
cout<<":-( Try again."<<endl;
memset (num,0,sizeof(num));
while (!sta.empty())
sta.pop();
find = true;
}
return 0;
}