通话记录(队列)
已知不超过 50条通话记录,通话记录有三种类型:0代表未接来电,1 代表已接来电,2 代表已拨电话。要求分别将三种类型前 10 条通话记录以三列的形式输出。
输入格式
输入不超过 50条通话记录,每条通话记录都占一行。每一行的第一个数字代表通话记录的类型,第二个数字代表电话号码,电话号码均由 11个数字组成。他们之间用一个空格隔开。
输出格式
分三列输出未接来电、已接电话和已拨电话。
每列之间用一个空格分割,最后一列后没有空格。每种类型输出前十个电话,先出现的通话记录先输出,不足十个的用 0 占位。
实现代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include<iostream> #include<string> #include<stdlib.h> using namespace std; typedef struct { string data[51]; int front,rear; }squeue,*squlink; void enqueue(squlink &Q,string s) { Q->rear+=1; Q->data[Q->rear]=s; } bool Emptyqueue(squlink Q) { if(Q->front==Q->rear ) return 1; else return 0; } string dequeue(squlink Q) { if(Emptyqueue(Q)) return "0"; else { Q->front =Q->front+1; return Q->data[Q->front]; } } int main() { squlink Q1,Q2,Q3; Q1=(squlink)malloc(sizeof(squeue)); Q1->front=0; Q1->rear =0; Q2=(squlink)malloc(sizeof(squeue)); Q2->front=0; Q2->rear =0; Q3=(squlink)malloc(sizeof(squeue)); Q3->front=0; Q3->rear =0; string str; while(getline(cin,str)) { if(str[0]=='0') { enqueue(Q1,str.substr(2,11) ); } if(str[0]=='1') { enqueue(Q2,str.substr(2,11)); } if(str[0]=='2') { enqueue(Q3,str.substr(2,11)); } } for(int i=0;i<10;++i) cout<<dequeue(Q1)<<" "<<dequeue(Q2)<<" "<<dequeue(Q3)<<endl; return 0; }
|
挺简单一道题,大概熟悉一下队列的操作
收藏Shaw的博客谢谢喵~