UVA-11321 - Sort! Sort!! And Sort!!!
02 Jun 2018
- UVa Online Judge 解題結果請於 Submit 後,參閱 uHunt。
- 如果你有任何建議與指教,歡迎於下方留言一起討論喔!
- 相同題目:ZOJ-d750 / NCTU-11069
題意概要
題目給定兩個整數 (), (),你要依照某些規則排序 個整數。先利用每個數字除以 的餘數由小到大排,若排序中比較的兩數為一奇一偶且兩數除以 的餘數相等,則奇數要排在偶數前面。若兩奇數除以 餘數大小相等,則原本數值較大的奇數排在前面。同樣的,若兩偶數除以 餘數大小相等,則較小的偶數排在前面。至於負數的餘數計算和 C 語言裡的定義相同,即負數的餘數絕對不會大於零。例如 -100 MOD 3 = -1
, -100 MOD 4 = 0
依此類推。
- 分析:本題為簡單的排序問題。按照題目給定的規則,使用
#include <algorithm>
中的sort
函式,並修改 Compare 函式中的判斷條件即可。請參考 我的程式碼。
Input
The input file contains sets of inputs. Each set starts with two integers () and () which denotes how many numbers are within this set. Each of the next lines contains one number each. These numbers should all fit in 32-bit signed integer. Input is terminated by a line containing two zeroes.
Output
For each set of input produce lines of outputs. The first line of each set contains the value of and . The next lines contain numbers, sorted according to the rules mentioned above. Print the last two zeroes of the input file in the output file also.
Sample Input
15 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0 0
Sample Output
15 3
15
9
3
6
12
13
7
1
4
10
11
5
2
8
14
0 0