Problem Type : Ad Hoc
#include<stdio.h>
int main()
{
int i,j,temp,a[1000],n,k,l,count,m;
while(scanf("%d",&n)==1)
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
count=0;
for(i=1; i<n; i++)
{
for(j=n-1; j>=i; j--)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
count=count+1;
}
}
}
printf("Minimum exchange operations : %d\n",count);
}
return 0;
}
#include<stdio.h>
int main()
{
int i,j,temp,a[1000],n,k,l,count,m;
while(scanf("%d",&n)==1)
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
count=0;
for(i=1; i<n; i++)
{
for(j=n-1; j>=i; j--)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
count=count+1;
}
}
}
printf("Minimum exchange operations : %d\n",count);
}
return 0;
}
No comments:
Post a Comment