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; }
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?