from itertools import product
import sys
provided_digits_str = "34679 123456789 013579 0345679"
print(f"输入字符串: \"{provided_digits_str}\"")
provided_parts = provided_digits_str.split()
total_positions = len(provided_parts)
if total_positions != 4:
print(f"错误:输入字符串 \"{provided_digits_str}\" 应包含 4 个部分(数字或 'x'/'X'),用空格隔开。")
sys.exit(1)
non_x_indices = []
x_indices = []
active_provided_digits = []
for i, part in enumerate(provided_parts):
if part.lower() == 'x':
x_indices.append(i)
else:
try:
digit_set = set(map(int, part))
non_x_indices.append(i)
active_provided_digits.append(digit_set)
except ValueError:
print(f"错误:输入字符串 \"{provided_digits_str}\" 的第 {i+1} 部分 \"{part}\" 包含无效字符。只允许数字和 'x'/'X'。")
sys.exit(1)
num_digits = len(non_x_indices)
num_x = len(x_indices)
if num_digits == 4:
mode_name = "四定"
combination_length_name = "4 位数"
elif num_digits == 3:
mode_name = "三定"
combination_length_name = "3 位数"
elif num_digits == 2:
mode_name = "两定"
combination_length_name = "2 位数"
elif num_digits == 1:
mode_name = "一定"
combination_length_name = "1 位数"
elif num_digits == 0:
mode_name = "零定"
combination_length_name = "0 位数"
else:
print("错误:出现意外情况,无法确定模式。")
sys.exit(1)
print(f"检测到模式: {mode_name} (共 {num_digits} 个非 'x' 位置)")
combinations = {i: [] for i in range(num_digits + 1)}
if num_digits > 0:
print(f"正在生成和检查所有 {combination_length_name} 组合 ({'0'*num_digits} 到 {'9'*num_digits})...")
for combo_tuple in product(range(10), repeat=num_digits):
matched_positions = 0
for i, digit in enumerate(combo_tuple):
allowed_set = active_provided_digits[i]
if digit in allowed_set:
matched_positions += 1
full_combo_list = [''] * total_positions
for idx in x_indices:
full_combo_list[idx] = 'X'
for i, idx in enumerate(non_x_indices):
full_combo_list[idx] = str(combo_tuple[i])
combo_str_full = "".join(full_combo_list)
combinations[matched_positions].append(combo_str_full)
elif num_digits == 0:
print("所有位置均为 'x',只有一个组合 'XXXX'。")
combinations[0].append('X' * total_positions)
print("检查完成,开始打印结果:\n")
for value in range(num_digits + 1):
combo_list = combinations[value]
count = len(combo_list)
print(f"--- 匹配 {value} 个位置的组合 (共 {count} 个) ---")
if combo_list:
print(" ".join(combo_list))
else:
print("(无)")
print()
print("所有结果打印完毕。")