Python Practice Example 69
Question:There are n people sitting in a circle, numbered in order. Starting from the first person, count off (from 1 to 3). Anyone who counts to 3 leaves the circle. Ask which original number remains at the end.
Program 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]
Run the above code, the output is:
$ python test.py 请输入总人数:34 10Other extensions
Python 100 Examples