title link : http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=2213
Thinking Analysis : This problem is similar to Joseph's problem; each time the top n cards are placed at the end of the queue, the entire queue can be treated as a ring, each moving forward n, then the number of the next card is numbered N.
and remove the card in the ring, continue n+1 step, so loop until the last card is numbered.
The code is as follows:
#include <iostream>using namespacestd;Const intMax_n = -;intDel[max_n];intNum[max_n];intMain () {intTimes ; CIN>>Times ; while(times--) { intn =0; intindex =-1; Memset (Del,0,sizeof(del)); memset (num,-1,sizeof(num));
Cin>>N; for(inti =0; I < n; ++i) {intTimes = i +1; intGo =Times ; while(GO) {index= (Index +1) %N; if(Del[index]! =1) Go--; } Index= (Index +1) %N; while(Del[index] = =1) Index= (Index +1) %N; Num[index]=Times ; Del[index]=1; } for(inti =0; I < n-1; ++i) cout<< Num[i] <<" "; cout<< Num[n-1] <<Endl; } return 0;}
Swjtu 2213 A Game about Cards (simulation questions)