Last active
August 29, 2015 14:08
-
-
Save shohan4556/1adc5c20f37ccbbf1e65 to your computer and use it in GitHub Desktop.
train swap uva 299 solution in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*------------------------------------------------*/ | |
//Uva Problem No: 299 | |
//Problem Name : Train Swapping | |
//Author : Shohanur Rahaman | |
//University : City University | |
//E-mail : [email protected] | |
//Problem Type: Sorting | |
/*-----------------------------------------------*/ | |
#include<stdio.h> | |
int main() | |
{ | |
int i,j,k,t_case,n,t,train[51],count; | |
while(scanf("%d",&t_case)==1){ | |
for(k=0;k<t_case;k++){ | |
count=0; | |
scanf("%d",&n); | |
for(i=0;i<n;i++) | |
scanf("%d",&train[i]); | |
for(i=0;i<n-1;i++) | |
for(j=0;j<n-i-1;j++) | |
if(train[j]>train[j+1]){ | |
count++; | |
t=train[j]; | |
train[j]=train[j+1]; | |
train[j+1]=t; | |
} | |
printf("Optimal train swapping takes %d swaps.\n",count); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment