【題解】CPE 一顆星選集:49. Sort! Sort!! and Sort!!!

題目

sort in ascending order of their modulo M
If M is same, odd(big first) first, then even(small first).

UVa 連結
ZeroJudge 連結

解法

#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大學程式能力檢定】目錄
發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *