約瑟夫環問題

#include<stdio.h>
#include<stdlib.h>node

/**
約瑟夫環
編號爲1,2,3……,n的n我的按順時針方向圍坐一圈,每一個人手中持有一個密碼。
開始任選一個報數上限正整數m,從第一我的開始按順時針方向自1開始報數,
報道m中止。報m的人出列,將手中的密碼做爲新m,從他下一我的從1開始從新數
數,循環,求最後剩下那我的的最初編號
**/
typedef struct Node{it

 int pwd;
 struct Node *next;
}Node;io


void exploer(Node * head, int num){循環

 if( head == NULL || head->next==NULL){ return ;}  
 while(1){密碼

  while(num >0){next

   if(head->next==head){ printf("exit\n");return ;}  
   num--;
   if(num==0){di

    num = head->next->pwd;
    printf("pwd is :%d\n",num);
    head->next=head->next->next;while

   }return

   

  }void

 }


}

int main(){


 int i=0,a=1,b=10;
   
        int pwd = (rand() % (b-a+1))+ a;

 Node *node = (Node *)malloc(sizeof(Node)*1);
 Node *head = node;
 Node *tmp =  NULL;
 node->pwd = pwd;
 printf(" start pwd is :%d\n",pwd);
 node->next= NULL;
 while( (i++) < 20){
  
         pwd = (rand() % (b-a+1))+ a;
  tmp = (Node*)malloc(sizeof(Node));
  tmp->pwd=pwd;
  tmp->next=NULL;
  node->next=tmp;
  node=tmp; 
  printf(" create pwd is :%d\n",pwd);

 } 
 node->next=head;
 exploer(head,1);

}

相關文章
相關標籤/搜索