/home/toolbox/public_html/solutions/2/299/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /* fprintf(stderr, "functionName: message", varslist); */
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2014-10-28
20 * Purpose: fun
21 * Problem: 299 - Train Swapping
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 int numberOfTimes;
29 int trainCnt;
30 int trains[51];
31
32 void init()
33 {
34 /* FUNCTION init */
35 scanf("%d ", &numberOfTimes);
36 } /* FUNCTION init */
37
38 void dump()
39 {
40 /* FUNCTION dump */
41 } /* FUNCTION dump */
42
43 void getInput()
44 {
45 /* FUNCTION getInput */
46 int i;
47
48 scanf(" %d ", &trainCnt);
49 for (i=0; i<trainCnt; i++)
50 {
51 /* for each train */
52 scanf(" %d ", &trains[i]);
53 } /* for each train */
54
55 } /* FUNCTION getInput */
56
57 void process()
58 {
59 /* FUNCTION process */
60 int i;
61 int k;
62 int swaps = 0;
63 int tmp;
64
65 for (i=0; i<(trainCnt-1); i++)
66 {
67 /* for i */
68 for (k=i+1; k<trainCnt; k++)
69 {
70 /* for k */
71 if (trains[i] > trains[k])
72 {
73 /* need to swap */
74 swaps++;
75 tmp = trains[i];
76 trains[i] = trains[k];
77 trains[k] = tmp;
78 } /* need to swap */
79 } /* for k */
80 } /* for i */
81 printf("Optimal train swapping takes %d swaps.\n", swaps);
82 } /* FUNCTION process */
83
84 int main ()
85 {
86 /* main */
87 int i;
88
89 init();
90 for (i=0; i<numberOfTimes; i++)
91 {
92 /* while */
93 getInput();
94 process();
95 } /* while */
96
97 return EXIT_SUCCESS;
98 } /* main */
99
100