原题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1088
#include <iostream>
using namespace std;
struct building
{
int num;
building *next;
};
int main()
{
int n,m;
while(cin>>n&&n!=0){
for(m=2;;m++){
building* head=NULL; //头节点
building* ps=NULL, //当前节点
* pn=NULL; //前一个节点
for(int i=0;i<n-1;i++){
ps=new building;
if(!head)
head=ps;
else
pn->next=ps;
ps->num=i+2;
pn=ps;
} //创建链表。
pn->next=head;
while(1){
for(int j=1;j<m-1;j++){
head=head->next;
}
if(head==head->next) break;
building* ps1=NULL; //要删除的节点。
ps1=head->next;
head->next=ps1->next;
head=ps1->next;
delete ps1;
}
if(head->num==2) break;
}
cout<<m<<'\n';
}
}
渣渣只会用链表做,开始还因为忘记把头指针置为NULL搞了好久,还好最后做出来了- -,顺便还学了一点调试技术。