## 一到简单的题关于STLqueue的简单运用
题目如下:
Cinema line
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?
InputThe first line contains integer n (1 ≤ n ≤ 105) — the number of people in the line. The next line contains n integers, each of them equals 25, 50 or 100 — the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.OutputPrint “YES” (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print “NO”
.Examples
Input
4
25 25 50 50
Output
YES
Input
225 100
Output
NO
Input
4
50 50 25 25
Output NO
大体意思:新电影《虎胆龙威》刚刚上映!电影院售票处有很多人在排队。他们每个人都有一张100、50或25卢布的钞票。一张“虎胆龙威”票价值25卢布。如果售票员一开始没有钱,并且严格按照人们排队的顺序售票,那么他可以向每个人售票并给找零吗?
我的思路:continue,break;语句。
具体我们按现实情况记录钱多少。代码如下:
#include<iostream>
using namespace std;
int main(){
int n,i,k=1,t1=0,t2=0,t3=0,a[100001];
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
if(a[i]==25)
{ t1++; //记录25的数目
continue;
}
if(a[i]==50)
{ if(t1>=1)
{
t1--;
t2++; //有了50就多一个50,少一个25,
continue;
}
else
{ k=0;
break;
}
}
if(a[i]==100) //两个方案
{
if(t1>=1&&t2>=1) //第一个找回50和25
{
t1--;
t2--;
continue;
}
if(t1>=3) //第二个找回三个25
{
t1=t1-3;
continue;
}
else
{ k=0;
break;
}
}
}
if(k==1)cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
不过我做完后听到一个同学说网上有一个用 STLqueue解决的方案。这里如下。注意以下在吗不是本人想出的。原创菜级。
#include <iostream>
#include<queue>
using namespace std;
int main()
{ int n,i;
int a[199999];
while(cin>>n)
{
queue<int> b;
queue<int> c;
int flag=1;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
if(a[i]==25)
b.push(1);
if(a[i]==50)
{
if(b.size()>0)
{
b.pop();
c.push(1);
}
else {cout<<"NO";flag=0; break;}
}
if(a[i]==100)
{
if(b.size()>0&&c.size()>0) {b.pop();c.pop();}
else if(b.size()>=3) {b.pop();b.pop();b.pop();}
else {cout<<"NO";flag=0;break;} }
}
if(flag) cout<<"YES";
}
return 0;}
————————————————
版权声明:这个是CSDN博主「菜圾」的原创代码,