Topic:
Set up a bank has a, b two business windows, and the speed of processing business is different, where a window processing speed is twice times the b window-that is, when a window is processed 2 customers, b window processing finished 1 customers. Given the customer sequence that arrives at the bank, export the customer sequence in the order in which the business is completed. It is assumed that the time interval of customer arrival is not taken into account, and a window customer first outputs when different Windows handle 2 customers simultaneously.
Input format:
The input is a positive integer, where the 1th digit n (≤1000) is the total number of customers, followed by the number of the N-digit customer. The number of odd-numbered customers need to go to a window to transact business, for even-numbered customers to the b window. The numbers are separated by a space.
Output format:
The number of the customer is output in the order in which the business processes are completed. The numbers are separated by a space, but the last number cannot have extra spaces.
Input Sample:
8 2 1 3 9 4 11 13 15
Sample output:
1 3 2 9 11 4 13 15
Ideas:
Create two queues, one customer for the a window, another customer for the b window, and a count of two windows. Since it is not considered the customer has arrived at the time interval, then the unified output can be.
- Output two customer of a window + a customer of a b window.
- When the client of a window is one and the customer of window B is greater than or equal to one, the client of the a window and a b window are output.
- Exits a loop when a or B two has a queue that is empty.
- Then output the client in the non-empty queue separately.
At first, the problem is too complicated to think about, then carefully consider the conditions given in the question, found that it is quite simple.
Code:
#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Constll MOD =2147493647;Const intMAXN = 1e5+Ten; typedefstructQnode {intdata; structQnode *Next;} Qnode,*Queueptr;typedefstruct{queueptr Frot; QUEUEPTR Rear;} Linkqueue;BOOLInitqueue (linkqueue&Q) {Q.frot= Q.rear = (queueptr)malloc(sizeof(Qnode)); if(!Q.frot) Exit (-2); Q.frot->next =NULL; return true;}BOOLDestroyqueue (linkqueue&Q) { while(Q.frot) {q.rear= q.frot->Next; Free(Q.frot); Q.frot=q.rear; } return true;}BOOLEnQueue (linkqueue& Q,inte) {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;}BOOLDeQueue (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;}BOOLIsEmpty (linkqueue&Q) { if(q.frot==q.rear) { return true; } return false;}intMain () {intN; Linkqueue A, B; Initqueue (A); Initqueue (B); inteven =0, odd =0, TMP; BOOLIsfirst =true; CIN>>N; for(intKK =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){ intA,b,c; DeQueue (A,a); DeQueue (A, b); DeQueue (B,C); Even-=2; Odd--; if(isfirst) {cout<<a<<" "<<b<<" "<<C; Isfirst=false; } Elsecout<<" "<<a<<" "<<b<<" "<<C; } Else if(even==1&& odd>=1){ intb; DeQueue (A,a); DeQueue (B,B); Even--; Odd--; if(isfirst) {cout<<a<<" "<<b; Isfirst=false; } Else{cout<<" "<<a<<" "<<b; } } } while(even) {intA; DeQueue (A,a); if(isfirst) {cout<<A; Isfirst=false; } Else{cout<<" "<<A; } even--; } while(odd) {intA; DeQueue (B,a); if(isfirst) {cout<<A; Isfirst=false; } Else{cout<<" "<<A; } Odd--; } return 0;}/*Sample Input: 8 2 1 3 9 4 11 13 15 Sample output: 1 3 2 9 each 4*/
View Code
7-1 Bank Business Queue Simple Simulation (25 points)