HDOJ 1702

#include <iostream>
#include <stack>
#include <queue>

using namespace std;

int main(int argc, char* argv[])
{
	int N,num,num1;
	char str[12];	
	cin>>N;
	while(N--)
	{
		cin>>num>>str;
	    //scanf("%d%s",&num,str);
		if('F'==str[2])
		{
			queue<int> b;                     //这个地方自己出现了问题
			while(num--)
			{
				cin>>str;
				if('I'==str[0])
				{
					cin>>num1;
					b.push(num1);
				}
				else
				{
					if(!b.empty())
					{
						cout<<b.front()<<endl;
						//printf("%d\n",b.front());
						b.pop();
					}
					else
						cout<<"None"<<endl;
				}
			}
		}
		else
		{
			stack<int> a;
			while(num--)
			{
				cin>>str;
				//scanf("%d %s",&num1,str);
				if('I'==str[0])
				{
					cin>>num1;
					a.push(num1);
				}
				else
				{
					if(!a.empty())
					{
						cout<<a.top()<<endl;
						a.pop();						
					}
					else
						cout<<"None"<<endl;
				}
			}		
		}
	}
	return 0;
}


找这个题目就是想要练习C++库中堆栈的使用的。

题目内容不是很难,自己犯了一个小错误,另有一个点现在还没有想通,看看提问会有神马收获没~

while(num--)
			{
				cin>>str;
				if('I'==str[0])
				{
					cin>>num1;
					b.push(num1);
				}


这段代码开始自己写成了下面的形式,结果多输入时是无限循环。自己对于cin的机制和应用还不了解,上网也没有找到相关的资料。有高手可以帮忙解答下不~O(∩_∩)O~

 

			
while(num--){
			cin>>str>>num1;
			if('I'==str[0])
			{
			 b.push(num1);
                           }			

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值