題目
sort in ascending order of their modulo M
If M is same, odd(big first) first, then even(small first).
解法
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
bool cmp(int a, int b){
if(a%m == b%m){
if(a%2 && b%2){
return a > b;
}else if(a%2==0 && b%2==0){
return a < b;
}else{
return a % 2;
}
}
return a%m < b%m;
}
int main(){
while(cin >> n >> m && n != 0 && m != 0){
int a[n];
for(int i=0; i<n; i++){
cin >> a[i];
}
sort(a, a+n, cmp);
cout << n << ' ' << m << '\n';
for(int i=0; i<n; i++){
cout << a[i] << '\n';
}
}
cout << "0 0\n";
}👉 回到:【CPE大學程式能力檢定】目錄