Pages

Monday 24 September 2012

UVA - 299 - Train Swapping (C solution)

#include <stdio.h>

int numofSwap(int arr[],int size){
        int counter=0,i,j;
        for(i=0;i<size-1;i++){
            for(j=i+1;j<size;j++){
                if(arr[i]>arr[j]){
                    counter++;
                }
            }
        }
        return counter;
    }

int main()
{   int cases,i,j;
    scanf("%d",&cases);
    for(i=0;i<cases;i++){
         int size;
         scanf("%d",&size);
         int arr[size];
         for(j=0;j<size;j++){
            scanf("%d",&arr[j]);
         }
         printf("Optimal train swapping takes %d swaps.\n",numofSwap(arr,size));
    }


       return 0;
}

2 comments:

  1. can you also post the solution for UVa704-colour hash in c/c++ or java? thanks

    ReplyDelete