UVA 299-Train Swapping
题目大意:给个序列只能交换相邻俩位,判断最小的交换次数
解题思路:模拟冒泡排序
#include <stdio.h>
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while(n--) {
int m;
int a[100];
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d", &a[i]);
}
int x = 0;
for(int i = 0; i < m - 1; i++)
for(int j = 0; j < m - i -1; j++)
if(a[j] > a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
x++;
}
printf("Optimal train swapping takes %d swaps.\n", x);
}
return 0;
}