poj 1674 Sorting by Swapping

poj 1674 Sorting by Swapping,第1张

poj 1674 Sorting by Swapping
#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define maxn 10005int a[maxn];int cnt, n;int findit(){    for (int i = 1; i <= n; i++)        if (a[i] != i) return i;    return -1;}int main(){    int t;    scanf("%d", &t);    while (t--)    {        scanf("%d", &n);        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);        cnt = 0;        int temp = 1;        while (1)        { if (a[temp] == temp)     temp = findit(); if (temp == -1)     break; swap(a[temp], a[a[temp]]); cnt++;        }        printf("%dn", cnt);    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4916453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存