#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;
}
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;
}
can you also post the solution for UVa704-colour hash in c/c++ or java? thanks
ReplyDeleteSure .. When I am free I will !!
Delete