C ++ classical algorithm problem - Joseph problems

26.Algorithm Gossip: Joseph problems (Josephus Problem)

Explanation

It is said that the famous Jewish historian Josephus had the following story: After the Roman occupation Qiaotapate, 39 Jews and Josephus and his friends hid in a cave, 39 Jews decided'd rather die than not to be an enemy to and decided a suicide, 41 individuals arranged in a circle, the first personal Countin, every third number reported to the people who have to commit suicide, then a re-count by the newspaper, until all suicide killed so far.

However, Josephus and his friends did not want to follow, Josephus wanted his friend to pretend to comply, he will arrange with his friends in the 16th and 31st position, then escaped this death game.

solution

Joseph algebraic analysis can be used to solve the problem, the problem will expand Okay, now suppose you and a friend Unfortunately m involved in this game, how you want to protect you and your friends? Draw two circles as long as you can make your own with a friend from death game, the two inner circles are arranged in order, while the outer ring is suicide sequence, as shown below:
Here Insert Picture Description

Using the program to solve, as long as an annular array to handle it by one count, every three to find no data area in the array to fill in a count, the count of 41 and straight up, then the array 1 starts from the index lists, each sequence can be a suicide that position, which is arranged Joseph, about 41 individual packets and Cardiff piano having 3 goes as follows:

14 36 1 38 15 2 24 30 3 16 34 4 25 17 5 40 31 6 18 26 7 37 19 8 35 27 9 20 32 10 41 21 11 28 39 12
22 33 13 29 23

From the foregoing, the last suicide was in the 31st position, while the penultimate suicide to be ranked in the 16th position, before people are killed off, so they do not know about the piano with his husband friends did not comply with the rules of the game.

#include<stdio.h>
#include<stdlib.h> 
#define N 41
#define M 3

    int main(void) {
        int man[ N] ={
            0
        } ;
        int count = 1;
        int i = 0, pos = -1;
        int alive = 0;

        while (count <= N) {
            do {
                pos = (pos + 1) % N; // 环状处理

                if (man[pos] == 0) i++;

                if (i == M) {    //  报数为3了
                    i = 0;
                    break;
                }
            } while (1);

            man[pos] = count;
            count++;
        }

        printf("\n约琴夫排列:");
        for (i = 0; i < N; i++)
            printf("%d ", man[i]);
        printf("\n\n您想要救多少人?");
        scanf("%d", & alive);

        printf("\nL表示这%d人要放的位置:\n", alive);
        for (i = 0; i < N; i++) {
            if (man[i] > alive) printf("D");
            else printf("L");
            if ((i + 1) % 5 == 0) printf("	");
        }
        printf("\n");
        return 0;
    }
Released 1133 original articles · won praise 928 · views 60000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/104019104