-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva299.cpp
49 lines (40 loc) · 955 Bytes
/
uva299.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* UVa - Probelm 299
*/
#include <iostream>
unsigned determineSwaps(unsigned train[51], unsigned length)
{
int swaps = 0;
for (int i=(length-1); i>=0; i--)
{
for (int j=0; j<i; j++)
{
if (train[j] > train[j+1])
{
int swap = train[j+1];
train[j+1] = train[j];
train[j] = swap;
swaps++;
}
}
}
return swaps;
}
int main()
{
unsigned numberCases;
std::cin >> numberCases;
for (int testCase=0; testCase<numberCases; testCase++)
{
unsigned numberCars;
std::cin >> numberCars;
unsigned train[51];
for (int car=0; car<numberCars; car++)
{
std::cin >> train[car];
}
int swaps = determineSwaps(train, numberCars);
std::cout << "Optimal train swapping takes " << swaps << " swaps." << std::endl;
}
return 0;
}