Computer Programming Contest Preparation

ToolBox - Source for: 2/299/train299.c



/home/toolbox/public_html/solutions/2/299/train299.c
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 
    4 /*
    5  Author: Damir Hasic
    6 
    7 */
    8 
    9 void swap(int array[], int y)
   10 {
   11     int temp;
   12 
   13     temp=array[y];
   14     array[y]=array[y+1];
   15     array[y+1]=temp;
   16 }
   17 
   18 void Process(int L, int array[])
   19 {
   20     int i;
   21     int x,y, swaps;
   22 
   23     swaps=0;
   24     for(x=0; x<L-1; x++)
   25         for(y=0; y<L-1-x; y++)
   26             {
   27                 if(array[y]>array[y+1])
   28                     {
   29                         swap(array,y);
   30                         swaps++;
   31                     }
   32             }
   33 
   34     printf("Optimal train swapping takes %d swaps.\n", swaps);
   35 }
   36 
   37 void Input(int array[], int L)
   38 {
   39     int N,num,i,j;
   40 
   41     scanf("%d", &N);
   42     for(i=0; i<N; i++)
   43         {
   44             scanf("%d", &L);
   45             for(j=0; j<L; j++)
   46                 {
   47                     scanf("%d", &num);
   48                     array[j]= num;
   49                 }
   50             Process(L, array);
   51         }
   52 }
   53 
   54 main()
   55 {
   56     int array[51], L;
   57     Input(array, L);
   58 }
   59