迴圈

輸入車廂

bubble sort

輸出次數

 

#include<iostream>

using namespace std;

 

 

int main()
{
    int N;
    int car[10000];
    int ans;

    while(cin >> N)
    {
        ans = 0;
        for(int i = 0; i<N; i++)
            cin >> car[i];

        for(int i = 0; i<N; i++)
            for(int j = 0; j<N-i-1; j++)
                if(car[j] > car[j+1])
                {
                    swap(car[j], car[j+1]);
                    ans++;
                }

        cout << ans << endl;
    }

    return 0;
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()