發表於 C++CPEUVA一顆星

【CPE】UVA 11321 Sort! Sort!! And Sort!!! 一顆星 by C++

11321 Sort! Sort!! And Sort!!!

簡易翻譯和條件:

首先會輸入兩個數字 N、M (0≤N ≤10000, 0≤M ≤10000)
N 代表 之後會輸入幾個需要排列的數字
M 代表需要被除的數字
接下來會輸入N個數字,每個數字一行
最後會輸入兩個0 代表測資結束

最後需要把 N M 輸出
接下來要輸出 那N個數字的特殊排列 和 兩個 0
特殊排列是甚麼呢?

特殊排列就是依照每個數字除M的餘數從小排到大
如果兩個數字%M一樣的話:
如果兩個數字都是基數的話 ⇒數字大的為優先
如果兩個數字都是偶數的話 ⇒數字小的為優先
如果兩個數字是一基一偶的話 ⇒基數為優先

就是這樣啦!
那基本上就是練習 sort function啦
可以用很原始又經典的排序法
或是用 #include 的 sort 和自定義比較大小的function啦

我的做法是利用 Vector 紀錄需要的所有數字之後
利用 sort 和 自定義 function 來計算排列先後
記得這邊需要用到 iterator 的概念喔!!(之後有機會我再寫幾篇來介紹!)
大概就是這樣啦

如果有任何問題或是你有更好的寫法歡迎再下面留言討論喔!
我們下一題見啦!

程式碼:

#include 
#include 
#include 

using namespace std;

int M=0, N=0;
vector arr;

inline bool isOdd(int n){
    return n%2!=0;
}

bool compare(int n1, int n2){

    int mod1 = n1%M, mod2 = n2%M;

    // if no ties happened
    if(mod1 != mod2) return mod1 n2;
    }else if(isOdd(n1) || isOdd(n2)){
        return isOdd(n1);
    }else if(isOdd(n1)==false && isOdd(n2)==false){
        return n1 > N >> M;
        if(N ==0 && M == 0) break;

        arr.clear();

        for(int i=0; i> _;
            arr.push_back(_);
        }

        // Sort
        sort(arr.begin(), arr.end(), compare);

        // Output
        cout << N << " " << M << endl;
        for(vector::iterator it=arr.begin(); it!=arr.end(); it++){
            cout << *it << endl;
        }
    }
    cout << ZERO << " " << ZERO << endl;

    return 0;
}

11321

作者:

一位 熱愛資工領域、喜歡好笑事物、偶爾打打網球 的學生 ! For A Better Me!

發表迴響

Please log in using one of these methods to post your comment:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.