您的位置:首页 > 其它

约瑟夫问题之求喊到3的退出

2016-07-20 17:21 399 查看
using namespace std;
typedef struct node
{
int data;
node * next;
}node;
node *creat()
{
node *head,*p;
head=(node*)malloc(sizeof(node));
head->data=0;
p=head;
for(int i=1;i<=10;i++)
{
node *s=(node*)malloc(sizeof(node));
s->data=i;
p->next=s;
p=s;
}
p->next=head->next;
return head;
}
int main()
{
int i=1;
node *head=creat();
//while(head!=NULL)
//{
//cout<<head->data<<endl;
//head=head->next;
//}

node *p;
p=head;
while(p!=p->next)
{
p=p->next;
if(i==2)
{
i=0;
p->next=p->next->next;
}
i++;
}
cout<<p->data<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: