自己写的,是利用列表 仿照报数过程一个一个把人踢出去最后得到结果:
count = int(raw_input('Please input the count:'))
li = []
for i in range(count):
li.append(i+1)
times = count/3 +2
count_tial = 0
for i in range(times):
for j in range(count_tial+1,len(li)+count_tial+1):
if count_tial == 2:
li[0] = 0
if (j - count_tial) < len(li) and (j+1)%3 == 0:
li[j - count_tial] = 0
count_0 = li.count(0)
try:
count_tial = len(li) - li.index(0,len(li)-3,len(li))-1
except Exception,e:
break
for i in range(len(li)):
try:
li.remove(0)
except:pass
if len(li) == 2:
print '\n ! ! ! The last is:',li[1],' ! ! !'
else:print '\n ! ! ! The last is:',li[0],' ! ! !'
print '\n(last li:',li,')'
下面代码打印出了踢人的过程:
count = int(raw_input('Please input the count:'))
li = []
for i in range(count):
li.append(i+1)
print li
times = count/3 +2
count_tial = 0
for i in range(times):
for j in range(count_tial+1,len(li)+count_tial+1):
if count_tial == 2:
li[0] = 0
if (j - count_tial) < len(li) and (j+1)%3 == 0:
li[j - count_tial] = 0
print 'set 0 li:',li
count_0 = li.count(0)
print 'count_0:',count_0
try:
print 'index of last 0:',li.index(0,len(li)-3,len(li))
count_tial = len(li) - li.index(0,len(li)-3,len(li))-1
except Exception,e:
break
print 'count_tial:',count_tial
for i in range(len(li)):
try:
li.remove(0)
except:pass
print 'after remove 0 :',li
if len(li) == 2:
print '\n\n ! ! ! The last is:',li[1],' ! ! !'
else:print '\n\n ! ! ! The last is:',li[0],' ! ! !'
print '\n(last li:',li,')'
大家如果运行后发现问题一定指出啊~