close

原文

中文

 

C++

#include <iostream>
using namespace std;

void swap (int *a,int *b)//交換的函式 
{
	int t=*a;
	*a=*b;
	*b=t;
}

int main()
{
	int i,t,n,l;
	
	while(cin>>n)//輸入測資數量 
	{
		
		while(n--)
		{
			cin>>l;//輸入數列項數 
			int num[l]={0},count=0;
			
			for(i=0;i<l;i++)//輸入數列 
			{
				cin>>num[i];
				
			}
			
			for(i=0;i<l;i++)//氣泡排列法 
			{
				for(t=0;t<l-i-1;t++)
				{
					if(num[t]>num[t+1])
					{
						swap(&num[t],&num[t+1]);//交換 
						count++;//計算次數 
					}
				}
			}
			
			cout<<"Optimal train swapping takes "<<count<<" swaps." <<endl;//輸出
		}
	}
    return 0;
}
 
arrow
arrow
    文章標籤
    UVa一星題 Train Swapping
    全站熱搜
    創作者介紹
    創作者 豪CO 的頭像
    豪CO

    程式道路,必為豐富

    豪CO 發表在 痞客邦 留言(0) 人氣()