【題解】ZeroJudge e533: 11942 – Lumberjack Sequencing

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e533
【解題想法】

  • 寫個bubble sort,只要過程需要swap,即使原數列未排序。
#include <iostream>
using namespace std;

int main() {
    int T, a[10];
    cin >> T;
    cout << "Lumberjacks:\n";
    while (T--){
        for (int i = 0; i < 10; i++){
            cin >> a[i];
        }
        bool flag = a[1] > a[0];
        bool ordered = true;
        for (int i = 1; i < 9; i++){
            for (int j = i+1; j < 10; j++){
                if (flag){
                    if (a[j] < a[j-1]){
                        ordered = false;
                        break;
                    }
                } else {
                    if (a[j] > a[j-1]){
                        ordered = false;
                        break;
                    }
                }
            }
            if (!ordered) break;
        }
        if (ordered) cout << "Ordered\n";
        else cout << "Unordered\n";
    }
    return 0;
}

分享本文 Share with friends