#45423: AC (0.01s, 1KB) 應該吧..... 利用bubble sort原理


1121228@stu.wghs.tp.edu.tw (你知道我是誰嗎!!??)

學校 : 臺北市立建國高級中學
編號 : 266561
來源 : [60.248.154.143]
最後登入時間 :
2025-03-28 09:48:03
a539. 10327 - Flip Sort -- UVa10327 | From: [218.172.27.142] | 發表日期 : 2025-02-28 21:58

#include <bits/stdc++.h>

using namespace std;

void bubbleSortMoves(auto v) {
    int n = v.size(), Minimum=0;
    for (int i = 0; i < n - 1; i++) {   // bubbleSort計算需要交換的次數
        for (int j = 0; j < n - i - 1; j++) {
            if (v[j] > v[j + 1]) {
                swap(v[j], v[j + 1]);
                Minimum++;
            }
        }
    }
    cout << "Minimum exchange operations : " << Minimum << endl;
}

int main() {
    int n, temp, m;
    vector<int> v;
    while (cin >> n) {
        v.clear();  // 確保每次都先規0
        v.shrink_to_fit();
        while (n--) {
            cin >> temp;
            v.push_back(temp);
        }
        bubbleSortMoves(v);
    }
    return 0;
}

 
ZeroJudge Forum