C Language Examples - Josephus Life-and-Death Mini-game
30 people are on a boat that is overloaded, and 15 people need to get off.
So the people line up in a queue, and their position in the queue is their number.
Count off starting from 1; the person who counts to 9 gets off the boat.
This repeats until only 15 people remain on the boat. Which numbered people got off the boat?
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
(
"
Number %d got off the boat
\
n
"
,
i
)
;
i
++;
c
++;
}
}
}
}
}
Running the above example, the output is:
第9号下船了
第18号下船了
第27号下船了
第6号下船了
第16号下船了
第26号下船了
第7号下船了
第19号下船了
第30号下船了
第12号下船了
第24号下船了
第8号下船了
第22号下船了
第5号下船了
第23号下船了
C Language Examples