http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=235
题意:
火车货物进行排序,升序;其实就是对数组进行排序操作。
解题:
对每个数组进行冒泡排序,并记录次数。水~
#include <iostream>
#include <stdio.h>
using namespace std;
// #define LOCAL_TEST
const int MAXN = 50;
int main()
{
#ifdef LOCAL_TEST
freopen("f:\\in.txt", "r", stdin);
freopen("f:\\out.txt", "w+", stdout);
#endif
int nCase;
int szNum[MAXN];
// get number of case
cin >>nCase;
while ( nCase-- )
{
// get all data, save to szNum
int n;
cin >>n;
for ( int i=0; i<n; i++ )
cin >>szNum[i];
// bubble method and count swaping
int nCount = 0;
for ( int i=0; i<n; i++ )
{
for ( int k=i+1; k<n; k++ )
if ( szNum[i] > szNum[k] )
{
int tmp = szNum[i];
szNum[i] = szNum[k];
szNum[k] = tmp;
nCount++;
} // end if
} // end for
// output
cout <<"Optimal train swapping takes " <<nCount <<" swaps." <<'\n';
} // end while
return 0;
}