uva-299

就是冒泡排序,然后记录一下一共交换了多少次

原题:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=235&mosmsg=Submission+received+with+ID+11692850

#include <stdio.h>

int n;
int l;
int trains[60];

int bubbleSort()
{
	int i,j,count,temp,swp;

	count=0;
	for(i=0;i<l-1;i++)
	{
		swp=0;
		for(j=l-1;j>i;j--)
		{
			if(trains[j]<trains[j-1])
			{
				temp=trains[j];
				trains[j]=trains[j-1];
				trains[j-1]=temp;
				count++;
				swp=1;
			}
		}
		if(0==swp)
			return count;
	}

	return count;
}

int main()
{
	int i;

	scanf("%d",&n);
	while(n--)
	{
		scanf("%d",&l);
		for(i=0;i<l;i++)
			scanf("%d",&trains[i]);

		printf("Optimal train swapping takes %d swaps.\n",bubbleSort());	
	}
	return 0;
}


posted on 2013-05-01 21:44  EXLsunshine  阅读(184)  评论(0编辑  收藏  举报