简单排序题,求交换的最少次数
#include <stdio.h>
int count,n;
int carriage[60];
int find(int x)
{
for(int i=1;i<=n;i++)
if(carriage[i]==x)
return i;
return -1;
}
void move(int x,int y)
{
for(int i=y-1;i>=x;i--)
{
carriage[i+1]=carriage[i];
count++;
}
}
int main()
{
int m;
scanf("%d",&m);
for(int cas=1;cas<=m;cas++)
{
count=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&carriage[i]);
for(int i=1;i<n;i++)
{
int pos=find(i);
if(pos!=i)
{
move(i,pos);
carriage[i]=i;
}
}
printf("Optimal train swapping takes %d swaps.\n",count);
}
return 0;
}
分享到:
相关推荐
FAS9000 - Hot-swapping the caching module
hot-swapping-compactpci
Leetcode经典01背包UVA Problem This repository include my answer about the UVA ...299 ...Train Swapping 10038 - Jolly Jumpers 10193 - All You Need Is Love -> 简单题,主要考察最大公约数问题g
FAS9000 - Swapping out a fan
FAS8200 - Swapping out a fan
AFF A300 - Swapping out a power supply.pdf
AFF A200 - Swapping out a power supply.pdf
AFF A300 - Swapping out a fan.pdf
AFF A800 - Swapping out a fan.pdf
AFF A320 - Swapping out a fan.pdf
FAS500f - Swapping out the power supply
FAS9000 - Swapping out a power supply
FAS2700 - Swapping out a power supply
FAS2600 - Swapping out a power supply
FAS8200 - Swapping out a power supply
AFF C190 - Swapping out a power supply.pdf
AFF A250 - Swapping out the power supply.pdf
AFF A800 - Swapping out a power supply.pdf
AFF A700s - Swapping out a power supply.pdf
Leakage Power Reduction by Multiple-VT Cells Swapping.pdf