地址:http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1349 

题目大意是给出1-n个数,并且这n个数为范围为1-n,并且各不相同。

直接模拟:

#include<cstdio>
#include<cstdlib>
#include<cmath>
int a[40010];
int main()
{
    int tcase,n,i,temp,ans,temp1;
    while(scanf("%d",&tcase)==1)
    {
        while(tcase--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                          scanf("%d",a+i);
            }
            ans=0;
            for(i=1;i<=n;i++)
            {
                temp=a[i];
                while(temp!=i)
                {
                    ans++;
                                    temp1=a[i];
                    a[i]=a[temp];
                    a[temp]=temp1;
                    temp=a[i];

                }
            }
            printf("%d\n",ans);





        }
    }
}

相关文章:

  • 2021-12-20
  • 2021-12-07
  • 2021-06-07
  • 2022-12-23
  • 2021-07-31
  • 2021-10-09
  • 2022-12-23
  • 2021-07-19
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-04-14
  • 2022-12-23
相关资源
相似解决方案