UVA - 299 Train Swapping

来源:互联网 发布:淘宝网阿里巴巴找密码 编辑:程序博客网 时间:2024/05/19 20:37

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

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

#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 0
原创粉丝点击