hdu_1276士兵队列训练问题(list)
1 #include<iostream> 2 #include<list> 3 using namespace std; 4 int main(){ 5 int t,n; 6 cin>>t; 7 while(t--){ 8 cin>>n; 9 int k=2; 10 list<int>mylist; 11 list<int>::iterator it; 12 for(int i=1;i<=n;i++) 13 mylist.push_back(i); //赋值,最后输出原始编号 14 while(mylist.size()>3){ 15 int num=1; 16 for(it=mylist.begin();it!=mylist.end();){ 17 if(num++%k==0) 18 it=mylist.erase(it); 19 else it++; 20 } 21 k==2?k=3:k=2; 22 } 23 for(it=mylist.begin();it!=mylist.end();it++){ 24 if(it!=mylist.begin()) 25 cout<<" "; 26 cout<<*it; 27 } 28 cout<<endl; 29 } 30 return 0; 31 }
注:使用双向链表list