UVA11111 Generalized Matrioshkas

题目意思就是一排套娃,里面的必须小于外面的,(不能等于)

做法就是用栈模拟,我是匹配到一个正数,并且这是栈顶是正数是 ,然后一直找到找到栈顶是这个数的相反数为止,把正数加起来与这个数比;;

代码有点乱。。不太想动了,,,


#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值