约瑟夫问题

简介: 约瑟夫问题:#include#include#define OK 1#define ERROR 0#define OVERFLOW -2typedef int status;typedef int ElemType;typedef stru...
约瑟夫问题:
#include<stdio.h>
#include<stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int status;
typedef int ElemType;

typedef struct CList{
   ElemType location;
   ElemType code;
   struct CList *next;
}CList,*ListPtr;

status InitList(ListPtr *CL);
status ReadList(ListPtr CL);
status Joseph(ListPtr CL);

void main(){
ListPtr L=NULL;
int cmd=0;

printf("******************************************************************\n");
printf("*     InitList--1      ReadList--2      Joseph--3     Exit--0    *\n");
printf("* Enter a operation code : 1, 2, 3, 0                            *\n");
printf("******************************************************************\n");

printf("\n------------------------------------------------------------------\n");
printf("\nOperation:");
scanf("%d",&cmd);

while(cmd!=0){
   switch(cmd){
      case 1:
         InitList(&L);
         break;
      case 2:
         ReadList(L);
         break;
      case 3:
         Joseph(L);
         break;
      }
   printf("\n\n------------------------------------------------------------------\n\n");
   printf("Operation:");
   scanf("%d",&cmd);
  }

}  

status InitList(ListPtr *CL){
   int count=0;
   int size=0;
   int cd=0;
   ListPtr p=NULL;
   ListPtr q=NULL;
   ListPtr r=NULL;

   printf("\nPlease enter the total numbers of people(1--32767):");
   scanf("%d",&size);
   if(size<1||size>32767){
      printf("Please check number(1--32767).\n"); 
      return ERROR;
      }
    else {
      q=malloc(sizeof(CList));
      if(!q)
         exit(OVERFLOW);
      count=1;
      p=q;
      p->location=count;
     
      do{
         printf("\nPlease enter NO.%d person's code:",count);       
         scanf("%d",&cd);
         if(cd<1||cd>32767)
            printf("\nIncorrect data,Try again.\n");
      }while(cd<1||cd>32767);

      p->code=cd;

      while(size>1)
      {
         r=malloc(sizeof(CList));
         if(!r)
            exit(OVERFLOW);
         r->location=++count;

         do{
            printf("\nPlease enter NO.%d person's code:",count);  
            scanf("%d",&cd);
            if(cd<1||cd>32767)
               printf("Incorrect data,try again.\n");
         }while(cd<1||cd>32767);

         r->code=cd;
         q->next=r;  
         q=r;
         size--;
      }

      *CL=q;
      q->next=p;

      return OK;
    }
}


status ReadList(ListPtr CL){
   int count=0;
   ListPtr p=NULL;
   
   if(!CL)
      return ERROR;
   else{
      p=CL->next;
      printf("\n[People:");
      printf("%d",p->location);
      printf("Code:%d]",p->code);
      p=p->next;

      while(p!=CL->next){
         count++; 
         if(count%3==0)
            printf("\n");
         else
            printf("\t");
         printf("[People:");
         printf("%d",p->location);
         printf("Code:%d]",p->code);
         p=p->next;
        }
   return OK;
   }
}

status Joseph(ListPtr CL){
   int n=0;
   int BeginNum=0;
   ListPtr p=NULL;
   ListPtr q=NULL;
   
   if(!CL)
      return ERROR;
   else {

      do{
         printf("\nPlease enter the begin number(1--32767):");
         scanf("%d",&BeginNum);
      if(BeginNum<1||BeginNum>32767)
         printf("\nIncorrect data,Try again.\n");
      }while(BeginNum<1||BeginNum>32767);

      p=CL;

      printf("\n-----------------The Output List is:-----------------------\n");

      while(p!=p->next){
         for(n=1;n<=BeginNum-1;n++)
            p=p->next;
         
         q=p->next;
         printf("%d  ",q->location);
         BeginNum=q->code;
         p->next=q->next;
         free(q); 
        }
      printf("%d  ",p->location);
      return OK;
     }
}

目录
相关文章
|
11月前
|
存储 算法
|
7月前
约瑟夫环问题的几种解法
约瑟夫环问题的几种解法
78 0
|
8月前
|
算法
约瑟夫环问题(三种方法)
约瑟夫环问题(三种方法)
101 0
|
12月前
|
算法 索引 Python
细究“约瑟夫环”
细究“约瑟夫环”
80 0
约瑟夫环问题
使用queue<int>q记得加上头文件#include<queue>
62 0
【线性表】洛谷P1996 约瑟夫问题
前言 本题来自洛谷P1996. 题目链接:约瑟夫问题 - 洛谷
60 0
|
算法 Java
算法 | 链表的应用,约瑟夫问题
算法 | 链表的应用,约瑟夫问题
104 0
算法 | 链表的应用,约瑟夫问题
约瑟夫环
题目: 已知n个人(以编号1,2,3--n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人有出列;以此规律重复下去,知道圆桌周围的人全部出列。输出出列顺序和最后剩下的人。
80 0

热门文章

最新文章