目标: 找到一个三位数的密码。 线索: 我们有四个提示: 318:一个数字正确且位置正确。 3...
Tạo vào: 2 tháng 3, 2025
Tạo vào: 2 tháng 3, 2025
目标: 找到一个三位数的密码。
线索: 我们有四个提示:
318:一个数字正确且位置正确。
379:一个数字正确但位置不正确。
863:两个数字正确但位置都不正确。
421:所有数字都不正确。
pythonpossible_codes = [] digits = "0123456789" for a in digits: for b in digits: for c in digits: code = a+b+c # skip if repeated digits are not allowed? The puzzle didn't say, so allow repetition # but from clue 4, we know digits 4,2,1 can't appear at all if '4' in code or '2' in code or '1' in code: continue # Now check clue 1: "318" => one digit correct & in correct place # We'll define a small function to check how many digits correct in correct place # and how many correct but in wrong place def match_clue(guess, real): correct_position = sum(g == r for g, r in zip(guess, real)) # to count correct but wrong place, we can do a bag-based approach # remove the ones that matched in correct position guess_unmatched = [] real_unmatched = [] for g,r in zip(guess, real): if g != r: guess_unmatched.append(g) real_unmatched.append(r) correct_wrong_place = 0 # now for each digit in guess_unmatched, if it's in real_unmatched, we remove one occurrence # and increment ru = list(real_unmatched) for g in guess_unmatched: if g in ru: correct_wrong_place += 1 ru.remove(g) return correct_position, correct_wrong_place cpos, cwrong = match_clue("318", code) if not (cpos == 1 and cwrong == 0): continue # Check clue 2: "379" => one digit correct but wrong place cpos, cwrong = match_clue("379", code) if not (cpos == 0 and cwrong == 1): continue # Check clue 3: "863" => two digits correct but all in wrong place cpos, cwrong = match_clue("863", code) if not (cpos == 0 and cwrong == 2): continue # Check clue 4: "421" => none correct # Actually we already excluded digits 4,2,1 from code, so it's automatically none correct # but let's just do the check cpos, cwrong = match_clue("421", code) if not (cpos == 0 and cwrong == 0): continue possible_codes.append(code) possible_codes
我们可以将这类“密码推理”题目的线索形式化,然后用穷举检验每个可能的三位数是否满足所有线索。根据题目给出的四条线索:
先做一些直接推理:
于是可将所有可能的三位数(0–9)中剔除含有4、2、1的组合,然后再依次检查是否满足前3条线索。下面给出一段思路/伪代码(这里直接用Python示例)来验证:
pythondef match_clue(guess, real): """ 返回两项: - correct_position: 位置和数字都匹配的个数 - correct_wrong_place: 数字对但位置不对的个数 """ correct_position = sum(g == r for g, r in zip(guess, real)) # 对未匹配的位置再做“数字是否存在”计数 guess_unmatched = [] real_unmatched = [] for g, r in zip(guess, real): if g != r: guess_unmatched.append(g) real_unmatched.append(r) correct_wrong_place = 0 for g in guess_unmatched: if g in real_unmatched: correct_wrong_place += 1 real_unmatched.remove(g) return correct_position, correct_wrong_place candidates = [] digits = "0123456789" for a in digits: for b in digits: for c in digits: code = a+b+c # 第4条线索: 不能包含4,2,1 if '4' in code or '2' in code or '1' in code: continue # 检查第1条线索:318 => 1 位对且位置对 cp, wp = match_clue("318", code) if not (cp == 1 and wp == 0): continue # 检查第2条线索:379 => 1 位对但位置不对 cp, wp = match_clue("379", code) if not (cp == 0 and wp == 1): continue # 检查第3条线索:863 => 2 位对但位置都不对 cp, wp = match_clue("863", code) if not (cp == 0 and wp == 2): continue # 如果都通过,则是候选答案 candidates.append(code) print(candidates)
实际跑起来,你会发现得到的唯一满足所有条件的三位数是:
最后我们也可以逐条验证“698”是否满足题意:
综上所述,698 即是唯一满足四条线索要求的三位密码。