josephus


Removal of every 'kth' person from a circle. Find the last remaining person

做~自己de王妃 提交于 2020-01-22 05:06:38
问题 As part of a recent job application I was asked to code a solution to this problem. Given, n = number of people standing in a circle. k = number of people to count over each time Each person is given a unique (incrementing) id. Starting with the first person (the lowest id), they begin counting from 1 to k. The person at k is then removed and the circle closes up. The next remaining person (following the eliminated person) resumes counting at 1. This process repeats until only one person is

Looping over numbers

落爺英雄遲暮 提交于 2020-01-01 04:44:05
问题 So this is the question that is given. You are in a room with a circle of 100 chairs. The chairs are numbered sequentially from 1 to 100. At some point in time, the person in chair #1 will be asked to leave. The person in chair #2 will be skipped, and the person in chair #3 will be asked to leave. This pattern of skipping one person and asking the next to leave will keep going around the circle until there is one person left, the survivor. And this is the answer I came up with. I believe this

“Josephus-p‌r‌o‌b‌l‌e‌m” using list in python

匆匆过客 提交于 2019-12-21 04:28:28
问题 I wanted to know if it will be possible to solve the Josepheus problem using list in python. In simple terms Josephus problem is all about finding a position in a circular arrangement which would be safe if executions were handled out using a skip parameter which is known beforehand. For eg : given a circular arrangement such as [1,2,3,4,5,6,7] and a skip parameter of 3, the people will be executed in the order as 3,6,2,7,5,1 and position 4 would be the safe. I have been trying to solve this

How to find “the first survivor” after a given position in the Josephus puzzle?

只愿长相守 提交于 2019-12-11 02:53:56
问题 I want to find the next survivor after a given position and number of people. (define renumber (lambda (position n) (if (< position 3) (+ position (- n 3)) (- position 3)))) (define survives? (lambda (position n) (if (< n 3) #t (if (= position 3) #f (survives? (renumber position n) (- n 1)))))) (define first-survivor-after (lambda (position n) (cond ((and (<= n 3)(<= position 3)) null) ((or (>= n 3)(>= position 3))(survives? position n) (if = #f survives?) (survives? (+ 1 position) n)

Solving Josephus with linked lists

社会主义新天地 提交于 2019-12-06 16:25:26
问题 I've been tryin for a while but i cant figure out how to make the program below take N as input and generate an M in order that the last soldier that dies is the 13th(N>13); int main() { int N, M; struct node { int player_id; struct node *next; }; struct node *p, *q; int i, count; printf("Enter N (number of players): "); scanf("%d", &N); printf("Enter M (every M-th payer gets eliminated): "); scanf("%d", &M); // Create circular linked list containing all the players: p = q = malloc(sizeof

Solving Josephus with linked lists

a 夏天 提交于 2019-12-04 22:08:17
I've been tryin for a while but i cant figure out how to make the program below take N as input and generate an M in order that the last soldier that dies is the 13th(N>13); int main() { int N, M; struct node { int player_id; struct node *next; }; struct node *p, *q; int i, count; printf("Enter N (number of players): "); scanf("%d", &N); printf("Enter M (every M-th payer gets eliminated): "); scanf("%d", &M); // Create circular linked list containing all the players: p = q = malloc(sizeof(struct node)); p->player_id = 1; for (i = 2; i <= N; ++i) { p->next = malloc(sizeof(struct node)); p = p-

Looping over numbers

筅森魡賤 提交于 2019-12-03 11:30:22
So this is the question that is given. You are in a room with a circle of 100 chairs. The chairs are numbered sequentially from 1 to 100. At some point in time, the person in chair #1 will be asked to leave. The person in chair #2 will be skipped, and the person in chair #3 will be asked to leave. This pattern of skipping one person and asking the next to leave will keep going around the circle until there is one person left, the survivor. And this is the answer I came up with. I believe this is the right answer, I've done it on paper about 10 times as well and came up with 74 every time. Is

Explanation for recursive implementation of Josephus problem

假装没事ソ 提交于 2019-12-03 02:50:01
问题 EDIT: n is the number of persons. k is the kth person being eliminated. So for k=2, every 2nd person is getting eliminated. int josephus(int n, int k) { if (n == 1) return 1; else return (josephus(n - 1, k) + k-1) % n + 1; } The code is as simple as it could be. But somehow I am unable to understand this problem (which is a little embarassing to be honest). The way I am trying to understand it is, josephus(n,k) gives the final solution for a population of size n and step size k. josephus(n,k)

Explanation for recursive implementation of Josephus problem

十年热恋 提交于 2019-12-02 16:24:19
EDIT: n is the number of persons. k is the kth person being eliminated. So for k=2, every 2nd person is getting eliminated. int josephus(int n, int k) { if (n == 1) return 1; else return (josephus(n - 1, k) + k-1) % n + 1; } The code is as simple as it could be. But somehow I am unable to understand this problem (which is a little embarassing to be honest). The way I am trying to understand it is, josephus(n,k) gives the final solution for a population of size n and step size k. josephus(n,k) can be calculated if we know the solution for josephus(n-1,k). That is in my opinion "optimal

Josephus sequence

蹲街弑〆低调 提交于 2019-11-29 12:31:50
问题 Description: There are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. I Googled this 'Josephus problem' and the Wikipedia

工具导航Map