C Language Example - Josephus Life and Death Game

C 语言实例C Language Examples

There are 30 people on a boat, it is overloaded, and 15 people need to get off.

So the people line up in a row, and their position in the line is their number.

They count off, starting from 1, and anyone who counts to 9 gets off the boat.

This repeats until only 15 people remain on the boat. The question is, which numbered people got off?

Example

#include<stdio.h> int c = 0; int i = 1; int j = 0; int a[30] = { 0 }; int b[30] = { 0 }; int main() { while (i<=31) { if (i == 31) { i = 1; } else if (c == 15) { break; } else { if (b[i] != 0) { i++; continue; } else { j++; if (j != 9) { i++; continue; } else { b[i] = 1; a[i] = j; j = 0; printf("No. %d got off the ship\n", i); i++; c++; } } } } }

Run the above example, the output result is:

第9号下船了
第18号下船了
第27号下船了
第6号下船了
第16号下船了
第26号下船了
第7号下船了
第19号下船了
第30号下船了
第12号下船了
第24号下船了
第8号下船了
第22号下船了
第5号下船了
第23号下船了

C 语言实例C Language Examples

other extensions