Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 69

Python Exercise Example 69

Python 100 Examples Python 100 Examples

**Problem:** n people stand in a circle and are numbered in order. Starting from the first person, they count off (from 1 to 3), and whoever counts to 3 leaves the circle. The question is: which original number is the last person remaining?

**Analysis:** None.

Program source code:

Example (Python 2.0+)

#!/usr/bin/python

-- coding: UTF-8 --

if

name

==

'

main

'

:

nmax

=

50

n

=

int

(

raw_input

(

'

Please enter the total number of people:

'

)

)

num

=

[

]

for

i

in

range

(

n

)

:

num

.

append

(

i

1

)

i

=

0

k

=

0

m

=

0

while

m

<

n

1

:

if

num

[

i

]

!=

0

:

k

+=

1

if

k

==

3

:

num

[

i

]

=

0

k

=

0

m

+=

1

i

+=

1

if

i

==

n

:

i

=

0

i

=

0

while

num

[

i

]

==

0

:

i

+=

1

print

num

[

i

]

Execute the above code; the output is:


$ python test.py 
请输入总人数:34
10

Python 100 Examples Python 100 Examples