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

来源:互联网 发布:js怎么取整数 编辑:程序博客网 时间:2024/05/17 04:30
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;}


1 0
原创粉丝点击