鸡蛋栈:

http://nyoj.top/problem/1140

鸡蛋队列:

http://nyoj.top/problem/1117

//鸡蛋栈 
#include<iostream>
#include<stack>
#include<string> 
using namespace std;

int main()
{
	int N;
	cin>>N;
	string p;
	stack<int>s;
	int a;
	while(N--)
	{
		int n;		
		int sum=0;
		cin>>n;
		for(int i=0;i<n;i++)
		{
// cin>>p>>a;
			cin>>p;
			if(p=="push")
			{//如果输入的是push,输入a 
				cin>>a;
				s.push(a);
			}
			else if(p=="pop")
			{//如果输入的是pop,不输入 
				s.pop();
			}
		}
		if(s.empty())
		{//如果空 
			cout<<"no eggs!"<<endl;
		 } 
		else
		{//如果非空 
			while(!s.empty())
			{//非空时 输出 
			cout<<s.top()<<" ";		
			s.pop();						
			}
			cout<<endl;	
		}
	}
	return 0;
 } 

//鸡蛋队列 
#include<iostream>
#include<queue>
#include<string>

using namespace std;

int main()

{
	int T;
	cin>>T;
	string p;
	queue<int> q;
	while(T--)
	{
		int N=0;
		cin>>N;
		while(N--)
		{			
			int a;
			cin>>p;
			if(p=="push")
			{//如果输入的是push 
				cin>>a;
				q.push(a);
			}
			else if(p=="pop")
			{//如果输入的是pop 
				if(!q.empty())
				{//仅在队列非空时有出队 
					q.pop();					
				 } 
			}
		}
		if(q.empty())
		{
			cout<<"no eggs!"<<endl;
		}
		while(!q.empty())
		{
			cout<<q.front()<<" ";
			q.pop();
		}
		cout<<endl;
	}
	return 0;
 } 
今天就不写解题思路了,本来这两道题只是想顺便练习下栈和队列的,没想到NYOJ测试用例坑太深,花了三四个小时wawawa,反正,emmm...各方面了解,这个题目两个大坑,总结一下:
  • (1):先判断是否为push,如果为push再输入,如果是pop不输入
  • (2)编号可能是无限大的数字
上面的两个代码我运用了这两天学习的stack和queue,写出来的这两个代码并不能过OJ平台,虽然本地完全ojbk.我也知道错在上面的第二条,如果改动需要用到截然不同的数组模拟方法,有悖初衷。青春短暂,就不在这一棵树上吊死了,我还是去换棵树吊吊吧。zzzzzz.........