#30133: 完整解答 (c++版)


(unknown)

School : No School
ID : 0
IP address : []
Last Login :
2023-03-28 23:50:18
b373. [福州19中]车厢重组 | From: [36.229.106.10] | Post Date : 2022-04-30 19:50

//將數列由小到大排列
//可能重複做超過一次,因此我用while來判斷啥時停,啥時不該停
#include<iostream>
using namespace std;
int main() {
    int n;
    while (cin>>n)
    {
        int test[10001] = {};
        for (int i = 0; i < n; i++) cin >> test[i];
        int count = 0, trigger = 0; //當trigger為0時就停下,1就繼續
        do {
            trigger = 0;
            /*將數列進行交換*/
            for (int i = 0; i < n; i++) {
                if (i != n - 1 && test[i] > test[i + 1]) {
                    int temp = test[i];
                    test[i] = test[i + 1];
                    test[i + 1] = temp;
                    trigger = 1;
                    count++;
                }
            }
            if (trigger == 0)break; //若這一輪都沒有進行交換,則離開while,輸出答案
        } while (trigger == 1);
        cout << count << endl;
    }
    return 0;
}

 
ZeroJudge Forum