Advertisement
skb50bd

Lab- 6 Problem 1

Jul 1st, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int N, L, S, t[50], i, j, min;
  7.  
  8.     cin >> N;
  9.  
  10.     while (N--){
  11.         cin >> L;
  12.         for (int i = 0; i < L; i++)
  13.             cin >> t[i];
  14.  
  15.         for (i = 0, S = 0; i < L - 1; i++){
  16.             min = t[i];
  17.             for (j = i + 1; j < L; j++){
  18.                 if (min > t[j]){
  19.                     S++;
  20.                     min = t[j];
  21.                     t[j] =  t[i];
  22.                     t[i] = min;
  23.                 }
  24.             }
  25.         }
  26.         cout << "Optimal train swapping takes " << S << " swaps." << endl;
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement