数据结构总结之队列
来源:互联网 发布:中小学网络答题活动 编辑:程序博客网 时间:2024/05/16 15:28
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; } }}/*23sdkjfksdjfdf12 58 985 6 82jdhfsdf58 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;}
阅读全文
0 0
- 数据结构总结之队列
- 数据结构之栈与队列学习总结
- 【数据结构】数据结构总结之线性表、栈和队列
- 数据结构-----队列总结
- 数据结构之队列(链式队列)
- 数据结构之队列(数组队列)
- 数据结构之队列-顺序队列
- 数据结构之循环队列
- 数据结构之顺序队列
- 数据结构之链队列
- java数据结构之队列
- 数据结构之队列
- 数据结构复习之【队列】
- 内核数据结构之队列
- 数据结构之队列实例
- 数据结构学习之队列
- 复习数据结构之队列
- C 队列之数据结构
- 147. Insertion Sort List
- KMP算法详解[自己觉得有价值的]
- python基础(04)——函数
- C语言
- 网络测试工具--Iperf、MZ
- 数据结构总结之队列
- Web of Science爬虫实战(模拟浏览器)
- 树莓派引脚相对位置示意图
- 瞎写点什么
- 【WebGL】茶壶和光照
- uva10142
- android的动画
- Unity 脚本接口值Path
- struts2框架搭建