1.
back()返回最后一个元素
empty()如果队列空则返回真
front()返回第一个元素
pop()删除第一个元素
push()在末尾加入一个元素
size()返回队列中元素的个数
2.队列题有时会涉及到带空格的字符串输入,数字输入,格式输入(也就是一行下还有一行空的,有间隔),比如:uva10142
//ac代码的部分代码(输入部分)
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std;
queue<int> q[1010];
char name[21][90];
int n;
int main()
{
int t;cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf(" %s",name[i]);
cout<<name[i]<<endl;
}
char vote[21];
int j=1;
getchar();
while(gets(vote) && vote[0])
{
int num;
int len=strlen(vote);
for(int k=0;k<len;k++)
{
num=0;
while(vote[k]!=' ' && vote[k]!='\0')
{
num+=vote[k]-'0';
num*=10;
k++;
}
num/=10;
q[j++].push(num);
cout<<num<<' ';
}
cout<<endl;
}
}
}
/*
2
3
sdkjfk
sdjf
df
12 58 98
5 6 8
2
jdhf
sdf
58 98
3.个人认为,队列的题可以用数组做。以前没有stl的时候,不就是用数组嘛?
比如uva144
数组代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <string.h>
using namespace std;
struct student
{
int num;
int payment;
} s[30];
int p;
int n,k;
int ok;
int in;
int out;
bool v[30];
int main()
{
while(scanf("%d%d",&n,&k) && !(n==0&&k==0))
{
p=1;
ok=0;
in=0;
out=0;
memset(v,0,sizeof v);
for(int i=1; i<=n; i++)
{
s[i].num=i;
s[i].payment=0;
}
while(1)
{
if(out==0)
{
out=in%k+1;
in++;
}
if(s[p].payment>=40-out)
{
out-=40-s[p].payment;
s[p].payment=40;
v[p]=true;
printf("%3d",p);
ok++;
if(ok==n) break;
p++;
if(p==n+1) p=1;
while(v[p])
{
p++;
if(p==n+1) p=1;
}
}
else
{
s[p].payment+=out;
out=0;
p++;
if(p==n+1) p=1;
while(v[p])
{
p++;
if(p==n+1) p=1;
}
}
}
cout<<endl;
}
return 0;
}
队列代码:
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct student
{
int num;
int payment;
};
queue <student> q;
int k,n;
struct Machine
{
int front_money;
int behind_money;
void convert();
void pay(student &s);
}machine;
void Machine::convert()
{
if(front_money==0)
{
behind_money=(behind_money++)%k+1;
front_money=behind_money;
}
}
void Machine::pay(student &s)//没有用引用类型,还是用cout发现的
{
if(front_money+s.payment>40)
{
front_money-=40-s.payment;
s.payment=40;
}
else
{
s.payment+=front_money;
front_money=0;
}
}
void initial()
{
for(int i=1;i<=n;i++)
{
student s;
s.payment=0;
s.num=i;
q.push(s);
}
machine.behind_money=0;
machine.front_money=0;
}
void start()
{
while(!q.empty())
{
machine.convert();
student s1=q.front();
machine.pay(s1);
q.pop();
if(s1.payment==40)
printf("%3d",s1.num);
else
q.push(s1);
}
}
int main()
{
while(scanf("%d%d",&n,&k) && !(n==0&&k==0))
{
initial();
start();
cout<<endl;
}
return 0;
}