Address
Click to open the link
That is, the loop removes those that can be divided by 2, those that can be divided by 3, and those that can be divided by 2, knowing that there are only 3 positions left:
Code:
#include<iostream> #include<cstdio> #include<string.h> #include<stack> #include<queue> using namespace std ; #define MAX 105 struct Node{ int num ; int ranks; }; queue<Node> qe ; int main(){ int Num,nums,i,j ; scanf("%d",&Num); while(Num--) { cin >>nums; if(nums<=3) { cout << 1 ; for(i=2 ; i <=nums; i ++) { cout << " "<<i ; } cout <<endl; continue ; } for(i = 1 ; i <=nums; i ++) { Node temp; temp.num=i; temp.ranks = i ; qe.push(temp); } int index = 1 ,base ; while(qe.size()>3) { int we= 1 ; if(index%2==1) base = 2 ; else base = 3 ; index ++ ; int qe_size = qe.size(); for(i = 1 ; i <= qe_size ;i++) { Node tp = qe.front(); qe.pop(); if(tp.ranks%base!=0) { tp.ranks = we++; qe.push(tp); } } } printf("%d",qe.front().num); qe.pop(); for(i=0 ; i <=qe.size(); i++) { printf(" %d",qe.front().num); qe.pop(); } printf("\n"); } }