# Flip Sort(UVA10327) ## 題目 [點我](https://onlinejudge.org/external/103/10327.pdf) ## 解題網站 [UVA](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1268) [ZJ](https://zerojudge.tw/ShowProblem?problemid=a539) ## 演算法 這題的題目敘述就是氣泡排序法(bubble sort),使用bubble sort簡單版的演算法,[這篇](https://hackmd.io/@Renektonn/r19h-4QWyx/edit)有提到: ``` 1. 當輸入n 1.1. 輸入n個數字到arr 1.2. 對arr做氣泡排序(由小到大),記錄交換的次數cnt 1.3. 依據格式,輸出cnt ``` ## 程式碼 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int n; int arr[1001]; while (cin >> n) { for (int i = 1; i <= n; i++) { cin >> arr[i]; } // bubble sort int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - 1; j++) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); cnt++; } } } cout << "Minimum exchange operations : " << cnt << endl; } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up