0
点赞
收藏
分享

微信扫一扫

UVA - 299 Train Swapping


题目大意:给出火车的车厢大小,要求从小到大排序,并记录移动的次数

解题思路:用冒泡排序法,再用一个变量记录移动次数

#include<cstdio>

int main() {

	int test;
	int number[100];
	scanf("%d", &test);
	int num;
	for(int i = 0; i < test; i++) {
		scanf("%d", &num);
		
		for(int j = 0; j < num; j++)
			scanf("%d", &(number[j]));
		int count = 0;
		for(int j = 0; j < num; j++)
			for(int k = j + 1; k < num; k++)
				if(number[j] > number[k]) {
					int temp;
					temp = number[j];
					number[j] = number[k];
					number[k] = temp;
					count++;	
				}
		printf("Optimal train swapping takes %d swaps.\n", count);
	}
	return 0;

}



举报

相关推荐

0 条评论