poj 1647Sorting by Swapping

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num[10010];
int main(){
    int t,n,m,i,s,j;
    scanf("%d",&t);
    while(t--){
        memset(num,0,sizeof (num));
        scanf("%d",&n);
        for(i=1;i<=n;i++){
            scanf("%d",&num[i]);
        }
        for(s=0,i=1;i<=n;i++)
        {
            if(num[i]==i) continue ;
            else{
                s++;
                for(j=i;j<=n;j++)
                {
                    if(num[j]==i){
                        num[j]=num[i];
                        break;
                    }
                }
            }
        }
        printf("%d\n",s);
    }
    return 0;
}

你可能感兴趣的:(poj 1647Sorting by Swapping)