---
# System prepended metadata

title: '(YKL33(UVA299)) Train Swapping :'
tags: [CPP(CPE)]

---

# (YKL33(UVA299)) Train Swapping :  
YKL33_UVA299_Train_Swapping.cpp

這一題很簡單 CPE的英文沒看 看他要求輸出 就可以看懂了
就是說 要 bubblesort 要 swap 幾次
```cpp!=
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        // input times
        int a;
        cin >> a;
        // input array
        int arr[a];
        for (int j = 0; j < a; j++)
        {
            cin >> arr[j];
        }
        // bubble sort
        int count = 0;
        for (int k = 0; k < a; k++)
        {
            for (int l = 0; l < a - k - 1; l++)
            {
                if (arr[l] > arr[l + 1])
                {
                    swap(arr[l], arr[l + 1]);
                    count++;
                }
            }
        }
        // output
        printf("Optimal train swapping takes %d swaps.\n", count);
    }
}
```

![image](https://hackmd.io/_uploads/Bk2LseiaC.png)
![image](https://hackmd.io/_uploads/rkxdies6R.png)
