您的位置:首页 > 移动开发

UVA - 299 Train Swapping

2016-07-09 18:29 531 查看
题目大意:交换相邻车厢使其最终编号从小到大排列,求要交换机次。

解题思路:冒泡排序加个统计次数的变量。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
int num[100];
int main() {
int N;
scanf("%d", &N);
while(N--) {
int L;
scanf("%d", &L);
int count = 0;
for (int i = 0; i < L; i++)
scanf("%d", &num[i]);
for (int i = 0; i < L-1; i++)
for (int j = 0; j < L-i-1; j++)
if (num[j] > num[j+1]) {
int t = num[j];
num[j] = num[j+1];
num[j+1] = t;
count++;
}
printf("Optimal train swapping takes %d swaps.\n", count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva