poj 1674 Sorting by Swapping

来源:互联网 发布:大话手游数据互通列表 编辑:程序博客网 时间:2024/05/06 13:52
#include <iostream>#include <memory.h>#include <algorithm>using namespace std;int num[10010];int main(){    int tc, n, i, j, ans;    cin >> tc;    while (tc--){          cin >> n;          ans = 0;          memset(num, 0, sizeof(num));          for (i = 1; i <= n; i++){              cin >> num[i];          }                    //暴力解决:如果顺序相同就继续查找,否则就找出属于这个下标位置上的这个数,再交换位置!           for (i = 1; i <= n; i++){              if (num[i] == i)  continue;              for (j = i+1; j <= n; j++){                   if (num[j] == i){                        swap(num[i], num[j]);                        ans++;                        break;                   }              }          }          cout << ans << endl;              }    system("pause");}/*2031 2 352 3 5 4 166 5 2 3 1 455 4 3 2 11222 1*/