python 约瑟夫杯_Python 约瑟夫生者死者小游戏

#11

奇卡

ycz***18qijian@qq.com

0

参考方法:

peple =[]

for i in range(1,31):

person = 'person%d'%i

peple.append(person)

c = 0

max =30

t = 0

while max >15:

god = []

for pe in peple:

c += 1

if c % 9 == 0:

print(c,pe,'离开')

max -= 1

god.append(pe)

for pe in god:

peple.remove(pe)

print('留下人员',peple)

奇卡

奇卡

ycz***18qijian@qq.com8个月前 (01-22)

#10

看看python有多优雅

189***03000@189.cn

0

参考方法:

liver = list(range(1, 31))

index = 1

list_index = index - 1

while True:

if len(liver) <= 15:

break

else:

if index == 9:

print('{0}号下船了'.format(liver[list_index]))

liver.remove(liver[list_index])

index = 1

else:

index += 1

list_index += 1

if list_index == len(liver):

list_index = 0

看看python有多优雅

看看python有多优雅

189***03000@189.cn8个月前 (01-23)

#9

fanzhao

732***338@qq.com

0

参考方法:

people = range(1,31)

tempList = list()

timer = 1

flag = True

while flag:

for x in people:

if people.index(x) in tempList:

continue

if timer == 9:

tempList.append(people.index(x))

timer = 0

if len(people) - len(tempList) == 15:

flag = False

break

timer += 1

for x in tempList:

print(x+1,end=" , ")

fanzhao

fanzhao

732***338@qq.com8个月前 (01-23)

#8

百川

pos***163.com

5

约瑟夫环是很经典的题目。以下代码充分利用 Python 数据结构的特性,以求尽可能简洁。

people=list(range(30))

while len(people)>15:

i=1

while i<9:

people.append(people.pop(0))

i+=1

print('{:2d}号下船了'.format(people.pop(0)))

百川

百川

pos***163.com7个月前 (02-17)

#7

shohan

532***493@qq.com

0

上面写的有点复杂了,其实可以很简单的几行代码,如下直接定义成函数调用吧:

def yuesefu(nums, step, stay):

#参数 nums:人数,step: 数到几的步数,stay: 最后留下多少人

lists = list(range(1, nums+1))

check = 0

while len(lists) > stay:

for i in lists[:]:

check += 1

if check == step:

check = 0

lists.remove(i)

print("{}号下船了".format(i))

return lists

stays = yuesefu(30, 9, 15)

print("最后留下的人:", stays)

shohan

shohan

532***493@qq.com6个月前 (03-11)

#6

逆光追梦

303***66@qq.com

2

a=[x for x in range(1,31)]

b=[]

for x in range(135): #x最少要数9*15次才能数够15名下船人数

if (x+1)%9==0:

b.append(a[x-len(b)]) #把a里面被淘汰的人加入到b列表中

y=len(b)-1 #列表b里面每增加一个元素,a里面对应的人员就会自动往前面进一位后面前进的位数刚好是列表b的长度-1

a.remove(a[x-y]) #同时册除列表a里面被点中的人

else:

a.append(a[x-len(b)]) #把没有被点中的人重新加入到列表a 的后面

print(b)

运行的结果:

[9, 18, 27, 6, 16, 26, 7, 19, 30, 12, 24, 8, 22, 5, 23]

逆光追梦

逆光追梦

303***66@qq.com6个月前 (03-12)

#5

随遇而安

109***9506@qq.com

0

这个问题的关键在于每次循环只选出一个人。因为当留在船上的人数大于报数最大值的2倍时,若每次循环选出所有的可能数,则会造成结果中留在船上人少于目标值。将问题定义成函数如下:

def JosephDeathGame(total, onboard, step):

peoples = list(range(1, 31))

x = 0 #下船人数计数

y = 1 #报数计数

z = 0 #索引计数

while x < total - onboard:

if z > len(peoples)-1:

z = 0

if y == step:

print("{0}号下船了!".format(peoples.pop(z)))

z -= 1

y = 0

x += 1

y += 1

z +=1

JosephDeathGame(30, 15, 9)

随遇而安

随遇而安

109***9506@qq.com6个月前 (03-19)

#4

Python小白

141***7026@qq.com

1

把前面两位大神的代码简单综合了一下,更加简单且适用各种情况的是。

def yuesefu(nums,step,stay):

#nums:总人数 step:步长 stay:剩余的人数

lists = list( range(1,nums+1) )

#check = 0

while len(lists) > stay:

i = 1

while i

lists.append(lists.pop(0))

#print(lists)

i+=1

#print(i)

print("{:2d}号要下船了".format(lists.pop(0)))

stays = yuesefu(30,9,15)

Python小白

Python小白

141***7026@qq.com6个月前 (03-23)

#3

realmesir

rea***sir@126.com

0

约瑟夫环,也贴一下代码,这个代码逻辑和报数下船过程贴近:

people = list(range(1, 31))

i = 0

while len(people) > 15:

i += (9 - 1)

if i >= len(people):

i -= len(people)

print("{}号下船了".format(people[i]))

del people[i]

realmesir

realmesir

rea***sir@126.com5个月前 (03-29)

#2

季修梵

tim***o@outlook.com

0

GitHub上看到的解决方法,代码简单易懂,参考下。

def main():

persons = [x for x in range(1, 31)]

# 统计下船的人数

dropped = 0

while (dropped < 15):

print('下船的号数为:', persons[8])

# 对列表进行切片操作,每次截取前9位后的数据和前8位的数据

persons = persons[9:] + persons[0:8]

# print('截取后的列表数据为:',persons)

dropped += 1

print ('剩余的号数为:')

print (sorted(persons))

if __name__ == '__main__':

main()

季修梵

季修梵

tim***o@outlook.com4个月前 (05-16)

#1

Deng

chh***g@hotmail.com

0

用从字典里移除键值的方法来解决这个问题:

people={}

for x in range(1,31):

people[x]="p{}".format(x)

c = 0

pn = 30

skip = []

while pn >15:

for i in range(1,31):

if i not in skip: # 跳过在移除人员列表中的键值

c += 1 # 累计数的次数

if c%9 == 0: # 当数到9的整数倍时

print("{:2d}".format(i) + " " + "号下船了")

skip.append(i) # 创建移除人员列表

pn -= 1 # 人员名单长度因移除而缩短1

people.pop(i) # 移除相应人员

print ('\n留下人员', people)

Deng

Deng

chh***g@hotmail.com2个月前 (07-07)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值