設某銀行有A、B兩個業務窗口,且處理業務的速度不一樣,其中A窗口處理速度是B窗口的2倍 —— 即當A窗口每處理完2個顧客時,B窗口處理完1個顧客。給定到達銀行的顧客序列,請按業務完成的順序輸出顧客序列。假定不考慮顧客先后到達的時間間隔,並且當不同窗口同時處理完2個顧客時,A窗口顧客優先輸出。
輸入格式:
輸入為一行正整數,其中第1個數字N(≤1000)為顧客總數,后面跟着N位顧客的編號。編號為奇數的顧客需要到A窗口辦理業務,為偶數的顧客則去B窗口。數字間以空格分隔。
輸出格式:
按業務處理完成的順序輸出顧客的編號。數字間以空格分隔,但最后一個編號后不能有多余的空格。
輸入樣例:
8 2 1 3 9 4 11 13 15
輸出樣例:
1 3 2 9 11 4 13 15
解題思路:這道題的意思就是模擬隊列,將偶數的數字存到b窗口,將奇數的數字存到a窗口,而且是先進先出,所以可以采用隊列,將偶數的放在b隊列,將奇數的放在a隊列;然后每輸出兩個a隊列中的元素
再輸出一個b隊列中的元素;
還有注意的一個點就是,最后是a隊列中的元素結尾還是b隊列中的元素結尾,這個要分情況討論;主要是最后空格的規范問題;
解法一:模擬隊列
代碼如下:
1 #include<iostream> 2 using namespace std; 3 4 5 6 struct queue{ 7 int data[1005]; //用於存隊列的數據; 8 int tail; //用來記錄從尾部插入了多少個元素; 9 int head; //計數隊頭,方便取出隊頭的元素; 10 }; 11 12 void push(queue *q ,int tmp ) //實現隊列的push用法;傳入類型為 queue 的指針 q;傳入要入隊的數tmp; 13 { 14 q->tail ++; //每入隊一個元素,尾部計數器+1; 15 q->data[q->tail] = tmp; //將元素入隊; 16 } 17 18 int pop(queue *q) //取出隊列的數字,從前端取出;此時要利用head記數; 19 { 20 q->head++; //每取一個,就將head+1,記數; 21 return q->data[q->head]; //返回隊頭的元素; 22 } 23 24 void init(queue *q) //初始化; 25 { 26 q->tail = -1 ; //將尾和頭記數均初始化為-1; 27 q->head = -1; 28 } 29 30 int notEmpty(queue *q) //判斷隊列是否為空; 31 { 32 int i ; 33 if(q->tail == q->head) //如果尾和頭的記數一樣,則隊列為空; 34 { 35 i = 0; 36 } 37 else 38 i = 1; 39 return i; //判斷隊列不為空; 40 } 41 int counta = 0,countb = 0; //用counta 記錄入a隊列的個數;用countb 記錄入b隊列的個數; 42 int main() 43 { 44 queue a ; //定義,實例化a; 45 queue b; //定義,實例化b; 46 init(&a); //初始化a隊列,並要用引用符號; 47 init(&b); //初始化b隊列,並用引用符號; 48 int n ; 49 int c; 50 cin>>n; 51 for(int i = 0 ; i < n ; i++) 52 { 53 cin>>c; 54 if(c%2==0) 55 { 56 push(&b,c); //如果是偶數,則進b隊列; 57 countb++; //countb相應+1; 58 } 59 else 60 { 61 push(&a,c); //如果是奇數,則進a隊列; 62 counta++; //counta相應+1; 63 } 64 65 } 66 //分情況,注意最后到底是a隊列的數結尾還是b隊列的數結尾,這影響格式; 67 if(counta > countb *2) //counta大於兩倍countb,是a隊列的元素結尾,則a最后一個元素后面不能加空格; 68 { 69 while(notEmpty(&a)||notEmpty(&b)) //當a隊列不空或者b隊列不空時; 70 { 71 if(notEmpty(&a)) //a隊列不空; 72 { 73 cout<<pop(&a); 74 counta--; 75 if(counta != 0) 76 cout<<" "; 77 cout<<pop(&a); //輸出兩個a隊列的元素; 注意這里a最后一個元素后面不能加空格; 78 counta--; 79 if(counta != 0) 80 cout<<" "; 81 } 82 if(notEmpty(&b)) 83 { 84 cout<<pop(&b); //輸出一個b隊列的元素; 85 cout<<" "; 86 } 87 } 88 } 89 else 90 if(counta <= countb *2) //counta小於等於兩倍countb,是b隊列的元素結尾,則b最后一個元素后面不能加空格; 91 { 92 while(notEmpty(&a)||notEmpty(&b)) 93 { 94 if(notEmpty(&a)) 95 { 96 cout<<pop(&a); //輸出兩個a隊列的元素; 97 cout<<" "; 98 cout<<pop(&a); 99 cout<<" "; 100 } 101 if(notEmpty(&b)) 102 { 103 cout<<pop(&b); //輸出一個b隊列的元素,注意b隊列最后一個元素的后面不能加空格; 104 countb--; 105 if(countb != 0) 106 { 107 cout<<" "; 108 } 109 110 } 111 } 112 } 113 114 115 116 return 0; 117 }
解法二:直接用stl中的queue;
1 #include<iostream> 2 #include<queue> //隊列頭文件 3 using namespace std; 4 5 6 long long int b[10005]; 7 queue<int>qa; //定義一個qa隊列,用於存放奇數 8 queue<int>qb; //定義一個qb隊列,用於存放偶數; 9 int tmp1; //用於取出qa“每次最前” 的元素; 10 int tmp2; //用於取出qa“每次最前” 的元素; 11 int n ; 12 int main() 13 { 14 cin>>n; 15 for(int i = 0 ; i < n ; i++) 16 { 17 cin>>b[i]; 18 if(b[i]%2==0) 19 qb.push(b[i]); //如果為偶數,則放入qb隊列; 20 else 21 qa.push(b[i]); //如果為奇數,則放入qa隊列; 22 } 23 24 int counta = qa.size(); //用counta 記錄qa隊列的長度; 25 int countb = qb.size(); //用countb 記錄qb隊列的長度; 26 //分情況,注意最后到底是qa隊列的數結尾還是qb隊列的數結尾,這影響格式; 27 if(counta > countb *2) //counta大於兩倍countb,是qa隊列的元素結尾,則a最后一個元素后面不能加空格; 28 { 29 while(!qa.empty()||!qb.empty()) //當qa隊列不空或者qb隊列不空時; 30 { 31 if(!qa.empty()) //a隊列不空; 32 { 33 tmp1 = qa.front(); //取出qa 隊頭的元素; 34 cout<<tmp1; //輸出qa隊頭的元素; 35 qa.pop(); //將qa隊頭的元素去掉; 36 counta--; //qa長度-1; 37 if(counta != 0) //注意這里a最后一個元素后面不能加空格; 38 cout<<" "; 39 tmp1 = qa.front(); //再進行一遍上面的操作; 40 cout<<tmp1; 41 qa.pop(); 42 counta--; 43 if(counta != 0) 44 cout<<" "; 45 } 46 if(!qb.empty()) 47 { 48 tmp2 = qb.front(); //取出qb 隊頭的元素; 49 cout<<tmp2; //輸出qb隊頭的元素; 50 qb.pop(); //將qb隊頭的元素去掉; 51 cout<<" "; 52 } 53 } 54 } 55 else 56 if(counta <= countb *2) //counta小於等於兩倍countb,是b隊列的元素結尾,則b最后一個元素后面不能加空格; 57 { 58 while(!qa.empty()||!qb.empty()) //當qa隊列不空或者qb隊列不空時; 59 { 60 if(!qa.empty()) //qa隊列不空時; 61 { 62 tmp1 = qa.front(); //與上面的if類似,只是不需要注意尾部是否輸出空格; 63 cout<<tmp1; 64 qa.pop(); 65 cout<<" "; 66 tmp1 = qa.front(); 67 cout<<tmp1; 68 qa.pop(); 69 cout<<" "; 70 } 71 if(!qb.empty()) 72 { 73 tmp2 = qb.front(); //與上面的if類似,需要注意尾部是否輸出空格; 74 cout<<tmp2; 75 qb.pop(); 76 countb--; 77 if(countb != 0) 78 { 79 cout<<" "; 80 } 81 82 } 83 } 84 } 85 86 87 return 0; 88 }