【C语言】约瑟夫问题

约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。

例如:m=3,数到3的人就出队列,下一个人接着从1开始计数

#include <stdio.h>

#define ALL     100             //all people in the ring
#define OUT     3       //if count is out to ring
/*global array for all people in the ring*/
int people[ALL];
void init_ring(void)     //init people int the ring
{
        int i = 0;
        for(i = 0;i < ALL;i++)
        {
                people[i] = i + 1;
        }
        return;
}
void print_ring(void)
{
        int i = 0;
        for(i = 0;i < ALL;i++)
        {
                printf("%d ",people[i]);
        }
        printf("\n");
        return;
}
int main(void)
{
        int left; //how many people in the ring
        int counter;
        int i;
        printf("demo josephus ring problem\n");

        init_ring();

        print_ring();

        left = ALL;    //left = all people
        counter = 0;   //counter = 1,2,3
        i = 0;         //begin from [0]   k值
        while(1)
        {
                if(people[i] > 0)
                        counter++;
                /*if count to 3 ,then someone is out*/
                if(counter == OUT)
                {
                        left--;    //someone is out
                        printf("%d is out\n",people[i]);
                        people[i] = 0;//set this people out
                        counter = 0; //set counter = 0   重新计数
                        print_ring();
                }
                //printf("i = %d,counter = %d,left =%d\n",i,counter,left);

                //print_ring();

                i++;    //get next people

                if(left < 1)
                        break;
                if( i == ALL)
                        i = 0;
                //getchar();  //debug
        }
        printf("problem is finished\n");
        return 0;
}

方法二:利用静态数组(类似于链表,数组中存放的是下一个数据的位置)

people[i] : 1 2 3 4 5 6

next[i]:      1 2 3 4 5 0   (位置)

  1 #include <stdio.h>
  2 
  3 #define ALL    100              //all people in the ring
  4 #define OUT     3               //if count is out to ting
  5 /*global array for all people in the ring*/
  6 
  7 /*record the next people index*/
  8 int next[ALL];
  9 void init_ring(void)
 10 {
 11         int i = 0;
 12         for(i = 0;i <ALL;i++)
 13         {
 14                 next[i] = (i + 1) % ALL;
 15         }
 16         return;
 17 }
 18 
 19 void print_ring(void)
 20 {
 21         int i = 0;
 22         for(i = 0;i < ALL;i++)
 23         {
 24                 printf("%d ",next[i]);
 25         }
 26         printf("(next)\n");
 27         return;
 28 }
 30 int main(void)
 31 {
 32         int left; //how many people in the ring
 33         int counter = 0;
 34         int i = 0;
 35         int prev;
 36         printf("demo josephus ring problem\n");
 37 
 38         init_ring();
 39 
 40         print_ring();
 41 
 42         left = ALL; //left = all people
 43         counter = 0;  //counter = 1,2,3
 44         i = 0;      //begin from [0]
 45         prev = ALL - 1;  //0 de prev = 5;
 46         while(left > 0)
 47         {
 48                 //every step is useful
 49                 counter++;
 50                 /*if count to 3 ,then someone is out*/
 51                 if(counter == OUT)
 52                 {
 53                         left--; //someone is out
 54                         printf("%d is out\n",i + 1);
 55                         counter = 0; //set counter = 0
 56                         /*set  the next[]*/
 57                         next[prev] = next[i];
 58                         //print_ring();
 59                 }
                 //printf("i = %d,counter = %d,left =%d\n",i,counter,left);
 62 
 63                 prev = i;
 64                 i = next[i];    //get next
 65 
 66                 if( i == ALL)
 67                         i = 0;
 68                 //getchar();
 69         }
 70         printf("problem is finished\n");
 71         return 0;
 72 }

猜你喜欢

转载自blog.csdn.net/zjy900507/article/details/80921307