9 一个猜数字的解法(python实现)

不多说:如图
最好能定义几个函数,
最好能有通用性

attachments-2021-09-LJQm5rHz614c9829ddc9e.png

请先 登录 后评论

最佳答案 2021-09-23 23:07

  1. # tips = []
  2. # dic = {}
  3. # N = None

  4. # while True:
  5. #     try:
  6. #         N = int(input('共有多少個已知條件?請輸入:'))
  7. #         break
  8. #     except TypeError:
  9. #         print('輸入錯誤!')

  10. # for n in range(N):
  11. #     print('\n已知條件 %d'%(n+1))
  12. #     while True:
  13. #         try:
  14. #             num = tuple(map(int, str(input('已知條件號碼是?請輸入:'))))
  15. #             break
  16. #         except TypeError:
  17. #             print('輸入錯誤!')
  18. #     while True:
  19. #         try:
  20. #             code = int(input('有多少個數字是正確數字?請輸入:'))
  21. #             break
  22. #         except TypeError:
  23. #             print('輸入錯誤!')
  24. #     if code > 0:
  25. #         while True:
  26. #             try:
  27. #                 index = int(input('有多少個數字位置是正確的?請輸入:'))
  28. #                 break
  29. #             except TypeError:
  30. #                 print('輸入錯誤!')
  31. #     else: print('因為沒有數字正確,所以沒有位置正確!')
  32. #     dic['code'] = code
  33. #     dic['index'] = index
  34. #     tips.append([dic, num])

  35. # notches = len(tips[0][1])
  36. # combination = 10**notches
  37. # -----------------------------------------------------------------------------------------------------------------------

  38. # 以下是根據題目所給的 5 個已知條件,如果想自己定義輸入,可以運用以上代碼,方便輸入

  39. N = 5
  40. notches = 3
  41. combination = []

  42. for digit in range(10):
  43.     temp = []
  44.     for position in range(notches):
  45.         temp.append(digit)
  46.     combination.append(temp)

  47. # code 代表正確數字有多少? index 代表正確位置有多少? 後面是題給的提示號碼
  48. tips = [
  49.     [{'code': 1, 'index': 1}, (6, 8, 2)],
  50.     [{'code': 1, 'index': 0}, (6, 1, 4)],
  51.     [{'code': 2, 'index': 0}, (2, 0, 6)],
  52.     [{'code': 0, 'index': 0}, (7, 3, 8)],
  53.     [{'code': 1, 'index': 0}, (8, 7, 0)],
  54. ]

  55. for i in range(N):
  56.     # 數字正確,位置也正確
  57.     if tips[i][0]['code'] > 0 and tips[i][0]['index'] > 0:
  58.         A = list(tips[i][1])
  59.         for n, i in enumerate(A):
  60.             for j in range(notches):
  61.                 if n != j:
  62.                     combination[i][j] = 'X'
  63.     # 數字正確,位置不正確
  64.     elif tips[i][0]['code'] > 0 and tips[i][0]['index'] == 0:
  65.         A = list(tips[i][1])
  66.         for n, i in enumerate(A):
  67.             for j in range(notches):
  68.                 if n == j:
  69.                     combination[i][j] = 'X'
  70.     # 全部不正確
  71.     elif tips[i][0]['code'] == 0 and tips[i][0]['index'] == 0:
  72.         A = list(tips[i][1])
  73.         for n, i in enumerate(A):
  74.             for j in range(notches):
  75.                 combination[i][j] = 'X'

  76. # ans 為默認等下輸出答案,flag 為肯定答案不存在數字
  77. ans = ['X']*notches
  78. flag = []

  79. for n, i in enumerate(combination):
  80.     count = 0
  81.     for j in i:
  82.         if j == 'X':
  83.             count += 1
  84.     if count == len(i)-1:
  85.         ans[n] = n
  86.         flag.append(n)
  87.     elif count == len(i):
  88.         flag.append(n)
  89.     elif count == 0:
  90.         flag.append(n)

  91. # 這裡 ans = [0, 'X', 2],因為 0 和 2 已確定位置,剩下找出中間位置數字
  92. for n, i in enumerate(combination):
  93.     if n not in flag:
  94.         if combination[n].index('X') != ans.index('X'):
  95.             for j in range(notches):
  96.                 if ans[j] == 'X':
  97.                     ans[j] = n

  98. print('answer: '.title(), tuple(ans))

  99. # 以上代碼還不夠完善,只針對題目給出的函數,如果有特別案例比如:2個號碼正確,1個位置正確,另一個號碼不正確等,這些不確定因素不能用以上代碼
  100. # 希望有大神可以完善代碼,感恩
复制代码
请先 登录 后评论

其它 3 个回答

Bertha02


首先,生成list
import itertools
list1 = list(itertools.permutati**((0,1,2,3,4,5,6,7,8,9),3))

请先 登录 后评论
黄静丶
其次:排除没有一个正确的(7,3,8)
not_in_num = (7,3,8)
tmp_arr = []
for i in list1:
    if (not_in_num[0] not in i) or (not_in_num[1] not in i) or (not_in_num[2] not in i):
         tmp_arr.append[i]
请先 登录 后评论
千四儿

共同學習,你調用 itertools 函數裡的 permutati** 好像不適合,因為 permutati** 屬於不重複性組合
舉例:
  1. (7, 4, 3)
  2. (8, 3, 5)
复制代码

但是題目答案也有可能:
  1. (7, 4, 7)
  2. (8, 8, 5)
复制代码

重複數字,就好像行李箱的密碼鎖一樣,一共有一萬组才對
请先 登录 后评论
  • 4 关注
  • 0 收藏,320 浏览
  • 嫣大大 提出于 2021-09-23 23:07

相似问题