问题 H: 交换瓶子

完成排序

先按照原来的顺序来

然后找,换

#include <bits/stdc++.h>
using namespace std;
int n,a[10005],ans;
int main(){
   // freopen("in","r",stdin);
    ios::sync_with_stdio(0);
    while(cin >> n) {
        ans = 0;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        for (int i = 1; i <= n; i++) {
            if (a[i] != i) {
                for (int j = i; j <= n; j++) {
                    if (a[j] == i) {
                        swap(a[j], a[i]);
                        ans++;
                    }
                }

            }
        }
        cout << ans << endl;
    }

    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/xcfxcf/p/12467525.html