7-1 銀行業務隊列簡單模擬 (25 分)

題目:c++

設某銀行有A、B兩個業務窗口,且處理業務的速度不同,其中A窗口處理速度是B窗口的2倍 —— 即當A窗口每處理完2個顧客時,B窗口處理完1個顧客。給定到達銀行的顧客序列,請按業務完成的順序輸出顧客序列。假定不考慮顧客前後到達的時間間隔,而且當不一樣窗口同時處理完2個顧客時,A窗口顧客優先輸出。ide

輸入格式:spa

輸入爲一行正整數,其中第1個數字N(≤1000)爲顧客總數,後面跟着N位顧客的編號。編號爲奇數的顧客須要到A窗口辦理業務,爲偶數的顧客則去B窗口。數字間以空格分隔。code

輸出格式:blog

按業務處理完成的順序輸出顧客的編號。數字間以空格分隔,但最後一個編號後不能有多餘的空格。隊列

輸入樣例:ci

8 2 1 3 9 4 11 13 15it

輸出樣例:class

1 3 2 9 11 4 13 15循環

思路:

建立兩個隊列,一個存放A窗口的客戶,另外一個存放B窗口的客戶,並分別統計兩個窗口的人數。既然是不考慮顧客前後到達的時間間隔的話,那就統一輸出就能夠了。

  1. 輸出兩個A窗口的客戶 + 一個B窗口的客戶。
  2. 當A窗口的客戶爲一個的時候且B窗口的客戶大於等於一個的時候就輸出一個A窗口的客戶 + 一個B窗口的客戶。
  3. 當A或B兩個中有一個隊列爲空就退出循環。
  4. 而後單獨輸出那個不爲空的隊列裏的客戶就能夠了。

一開始把這個題想的太複雜了,後來仔細考慮了一下題中給出的條件,發現其實挺簡單的。

代碼:

#include <bits/stdc++.h>
using namespace std; typedef long long ll; const ll MOD = 2147493647; const int maxn = 1e5+10; typedef struct QNode { int data; struct QNode *next; } QNode,*QueuePtr; typedef struct { QueuePtr frot; QueuePtr rear; } LinkQueue; bool initQueue(LinkQueue& Q) { Q.frot = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if(!Q.frot) exit(-2); Q.frot->next = NULL; return true; } bool DestroyQueue(LinkQueue& Q) { while(Q.frot) { Q.rear = Q.frot->next; free(Q.frot); Q.frot = Q.rear; } return true; } bool EnQueue(LinkQueue& Q, int e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if(p==NULL) { exit(-2); } p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return true; } bool DeQueue(LinkQueue& Q,int& e) { if(Q.frot==Q.rear) return false; QueuePtr p = Q.frot->next; e = p->data; Q.frot->next = p->next; if(Q.rear == p) Q.rear == Q.frot; free(p); return true; } bool isEmpty(LinkQueue& Q){ if(Q.frot==Q.rear){ return true; } return false; } int main() { int n; LinkQueue A,B; initQueue(A); initQueue(B); int even = 0,odd = 0,tmp; bool isfirst = true; cin>>n; for(int kk = 0; kk<n; kk++) { cin>>tmp; if(tmp&1) { EnQueue(A,tmp); even++; } else { EnQueue(B,tmp); odd++; } } while(even&&odd){ if(even>=2 && odd>=1){ int a,b,c; DeQueue(A,a); DeQueue(A,b); DeQueue(B,c); even-=2; odd--; if(isfirst){ cout<<a<<" "<<b<<" "<<c; isfirst = false; } else cout<<" "<<a<<" "<<b<<" "<<c; } else if(even==1 && odd>=1){ int a,b; DeQueue(A,a); DeQueue(B,b); even--; odd--; if(isfirst){ cout<<a<<" "<<b; isfirst = false; } else{ cout<<" "<<a<<" "<<b; } } } while(even){ int a; DeQueue(A,a); if(isfirst){ cout<<a; isfirst = false; } else{ cout<<" "<<a; } even--; } while(odd){ int a; DeQueue(B,a); if(isfirst){ cout<<a; isfirst = false; } else{ cout<<" "<<a; } odd--; } return 0; } /* 樣例輸入: 8 2 1 3 9 4 11 13 15 樣例輸出: 1 3 2 9 11 4 13 15 */
View Code
相關文章
相關標籤/搜索