Uva--299 (排序)

2014-06-04 01:43:18

题意&思路:手动冒泡排序。

#include <cstdio>
#include <iostream>
using namespace std;

int main(){
    int Case,n,s[55],cnt;
    scanf("%d",&Case);
    while(Case--){
        scanf("%d",&n);
        for(int i = 1; i <= n; ++i){
            scanf("%d",&s[i]);
        }
        cnt = 0;
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n - i; ++j){
                if(s[j] > s[j + 1]){
                    int temp = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                    ++cnt;
                }
            }
        }
        printf("Optimal train swapping takes %d swaps.\n",cnt);
    }
    return 0;
}

 

posted @ 2014-06-04 01:44  Naturain  阅读(66)  评论(0编辑  收藏  举报