1 #include<iostream>//cheng da cai zi 08 .11 .13 2 using namespace std; 3 int main() 4 { 5 int digit_num; 6 int result=0; 7 int i; 8 int time; 9 cin>>time; 10 while(time--){ 11 result=0; 12 cin>>digit_num; 13 bool * already=new bool[digit_num+1]; 14 for(i=1;i<=digit_num;i++) 15 already[i]=false; 16 int * digit=new int[digit_num+1]; 17 for(i=1;i<=digit_num;i++) 18 cin>>digit[i]; 19 for(i=1;i<=digit_num;i++) 20 { 21 while(already[i]==false) 22 { 23 if(digit[i]==i) 24 already[i]=true; 25 else 26 { 27 already[digit[i]]=true; 28 result++; 29 digit[i]=digit[digit[i]]; 30 } 31 } 32 33 } 34 cout<<result<<endl; 35 delete []already; 36 delete []digit; 37 } 38 return 0; 39 }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com