Problem link : UVa 299 - Train Swapping
Solution :
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a[55],i,j,m,n,p,t,tc;
scanf("%d",&tc);
for(t=1;t<=tc;t++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
cin>>a[i];
m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
p=a[j];
a[j]=a[j+1];
a[j+1]=p;
m++;
}
}
}
printf("Optimal train swapping takes %lld swaps.\n",m);
}
return 0;
}
Solution :
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a[55],i,j,m,n,p,t,tc;
scanf("%d",&tc);
for(t=1;t<=tc;t++)
{
scanf("%d",&n);
for(i=0;i<n;i++)
cin>>a[i];
m=0;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
p=a[j];
a[j]=a[j+1];
a[j+1]=p;
m++;
}
}
}
printf("Optimal train swapping takes %lld swaps.\n",m);
}
return 0;
}
No comments:
Post a Comment