UVA - 540 Team Queue

来源:互联网 发布:淘宝怎么买翻墙 编辑:程序博客网 时间:2024/05/20 21:44

题目大意:按照要求设置一个队列,然后进行排队和出队,排队是有优先级的

解体思路:用一个解构体,存储数字和优先级

#include<cstdio>#include<cstring>#include<cstdlib>struct queue{int number;int mark;};int main() {int test;int ci = 1;while(scanf("%d", &test) != EOF) {queue q[100000];//存储传入的数字int count = 0;//记录存放了多少个数字if(test == 0)//为0时跳出循环break;printf("Scenario #%d\n", ci);for(int i = 0; i < test; i++) {//循环test个队列int num;scanf("%d", &num);for(int j = 0; j < num; j++) {//读取一个队列中的数long long temp;scanf("%lld", &temp);q[count].number = temp;q[count].mark = i;count++;}}queue que[100000];int rear = 0, front = 0;char str[1000];while(scanf("%s", str) && str[0] != 'S') {//读取命令if(str[0] == 'E') {int  n;queue mark;scanf("%d",&n);int num = 0 ;int m = 1;for(int i = 0; i < count; i++)if(q[i].number == n)mark = q[i];// 找到mark属于哪个队列int j;for( j = rear - 1; j >= front; j--) {if(que[j].mark == mark.mark) {num = j;break;}}if(j < front) {//没找到自己所在的队列que[rear++] = mark;}else {for(int k = rear - 1; k > num; k--)que[k+1] = que[k];que[num+1] = mark;rear++;}}else {printf("%d\n", que[front].number);front++;}}ci++;printf("\n");}return 0;}


0 0
原创粉丝点击