完成排序
先按照原来的顺序来
然后找,换
#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; }