鱼C论坛

 找回密码
 立即注册
查看: 2156|回复: 1

[学习笔记] python解决魔术师发牌问题

[复制链接]
发表于 2019-4-19 14:37:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题简介
https://jingyan.baidu.com/article/0aa223755363c688cc0d6485.html

python的数据类型里没找到单循环链表的 又不想自己写 就用现成的列表类型改了下
参数是牌的长度 默认13

  1. def p1(num=13):
  2.     list1 = [0]*num
  3.     index = 0
  4.     count = 1  # 翻牌次数
  5.     while 0 in list1:
  6.         if count == 1:
  7.             list1[index] = count
  8.             count += 1
  9.             continue
  10.         if count > 1:   
  11.             for i in range(count):
  12.                 index += 1
  13.                 if index > len(list1)-1:
  14.                     index -= len(list1)
  15.                 while list1[index] != 0:
  16.                     index += 1
  17.                     if index > len(list1)-1:
  18.                         index -= len(list1)
  19.             list1[index] = count
  20.             count += 1
  21.     return list1
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-4-19 15:36:15 | 显示全部楼层
确实不擅长搞这种烧脑的,没看你答案前我试着自己弄,。。。。。。。。。搞了30分钟才出来思路了
  1. max = 13
  2. list = []
  3. while True:
  4.     if len(list) == 0:
  5.         list.append(max)
  6.     elif len(list) == max:
  7.         break
  8.     else:
  9.         temp = max - len(list)
  10.         list.insert(0,temp)
  11.         count = temp -1
  12.         while count:
  13.             list.insert(0,list.pop())
  14.             count -= 1

  15. print(list)
复制代码

不想起变量名,用了内置的函数作为变量名
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 12:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表