It is said that the famous Jewish historian Joseph PHUs had the following story: After the Romans occupied jotabat, 39 Jews and Joseph and his friends hid in a cave, the 39 Jews decided to die rather than be caught by the enemy, so they decided a method of suicide. 41 people lined up in a circle and reported by 1st people, each time the number reaches 3rd, the person must commit suicide, and then report the number again from the next one until all the people commit suicide. However, Joseph and his friends do not want to follow suit. First start from a person, cross the K-2 individual (because the first person has been crossed), and killKIndividual. Next, go over the K-1 and killKIndividual. This process continues along the circle until only one person remains. The problem is, given the sum, where should we start to avoid being executed? Joseph asked his friends to pretend to follow the game. He arranged his friends and himself in 16th and 31st positions, so he escaped the game of death. The 17th century French mathematician Gasper told the story of 15 believers and 15 non-believers in deep-sea distress in the number of game issues, half of the people must be put into the sea, and the rest of the people can survive, so I thought of a way: 30 people in a circle, from the first person to report, the ninth person throws him into the sea and repeats until only 15 people exist. Ask how to arrange the law so that every time you invest in the sea, it will be non-believers. * Problem analysis and algorithm design Joseph's problems are not difficult, but there are many ways to solve them. There are also many forms of questions. Here is an implementation method. 30 people in the question circle, which inspires us to use a cyclical chain. The structure array can be used to form a loop chain. There are two members in the structure. One is the pointer to the next person to form a ring chain. The other is the mark of whether the person is thrown down the sea. The other is 1, indicating that the person is still on the ship. From the first person, count the person who has not yet dropped the sea. When the number is 9, the mark in the structure is changed to 0, indicating that the person has been dropped to the sea. In this way, the cycle is counted until 15 people are dropped. Joseph's problem is a famous one: N people are in a circle, starting from the first to report the number, M will be killed, and the last one will be killed. For example, N = 6, M = 5, and the number of the person killed is 5, 4, 6, 2, 3. The last part is the 1st. Assume that the first K are good people in the circle, and the last K are bad people. Your task is to determine such a minimum M, so that all bad people will be killed before the first good person. Solution:
/*************************************************************************> File Name: josefu.c> Author: Baniel Gao> Mail: createchance@163.com > Blog: blog.csdn.net/createchance > Created Time: Thu 19 Dec 2013 03:08:21 PM CST ************************************************************************/#include
#include
typedef struct josefu{int data;struct josefu *next;} josefu_list;josefu_list *josefu_init(int num);josefu_list *josefu_begin(josefu_list *list, int rule);josefu_list *josefu_node(int num);int josefu_free(josefu_list *list);int main(void){josefu_list *list;int num, rule;puts("Please input number and rules: ");scanf("%d%d", &num, &rule);list = josefu_init(num);list = josefu_begin(list, rule);josefu_free(list);return 0;}josefu_list *josefu_init(int num){int i;josefu_list *new;josefu_list *list = josefu_node(1);for(i = num; i > 1; i--) {new = josefu_node(i);new->next = list->next;list->next = new;}return list;}josefu_list *josefu_node(int num){josefu_list *p = NULL;p = (josefu_list *)malloc(sizeof(josefu_list));p->data = num;p->next = p;return p;}josefu_list *josefu_begin(josefu_list *list, int rule){int counter;josefu_list *tmp = NULL;for(counter = 1; list->next != list; counter++) {if(counter == rule - 1) {tmp = list->next;list->next = tmp->next;free(tmp);counter = 0;josefu_show(list);}list = list->next;}return list;}int josefu_show(josefu_list *list){josefu_list *p = list;if(list == NULL)return 0;do {printf("%5d",p->data);p = p->next;} while(list != p);putchar('\n');}int josefu_free(josefu_list *list){free(list);return 0;}
Running instance: