fork download
  1. # 配置参数
  2. TARGET_SUM = 72602 # 目标总和
  3. MULTIPLIER = 620 # 乘数
  4. NUMBER_TO_SPLIT = 117.1 # 原始数字
  5. SPLIT_PARTS = 8 # 拆分份数
  6. MIN_PRODUCT = 6000 # 最小乘积
  7. MAX_PRODUCT = 10000 # 最大乘积
  8.  
  9. def split_numbers():
  10. # 基础验证
  11. if not isclose(NUMBER_TO_SPLIT * MULTIPLIER, TARGET_SUM):
  12. print("参数不匹配: 原始数字×乘数 ≠ 目标总和")
  13. return None
  14.  
  15. min_total = SPLIT_PARTS * MIN_PRODUCT
  16. max_total = SPLIT_PARTS * MAX_PRODUCT
  17. if TARGET_SUM < min_total or TARGET_SUM > max_total:
  18. print(f"无法拆分: 目标总和需在[{min_total}, {max_total}]范围内")
  19. return None
  20.  
  21. # 生成初始乘积
  22. step = (MAX_PRODUCT - MIN_PRODUCT) // (SPLIT_PARTS - 1) if SPLIT_PARTS > 1 else 1
  23. products = [MIN_PRODUCT + i * step for i in range(SPLIT_PARTS)]
  24.  
  25. # 调整至目标总和
  26. current_sum = sum(products)
  27. diff = TARGET_SUM - current_sum
  28. i = 0
  29. direction = 1 if diff > 0 else -1
  30.  
  31. while diff != 0:
  32. if MIN_PRODUCT <= products[i] + direction <= MAX_PRODUCT and (products[i] + direction) not in products:
  33. products[i] += direction
  34. diff -= direction
  35. i = (i + 1) % SPLIT_PARTS
  36.  
  37. # 计算拆分值
  38. split_values = []
  39. for p in products:
  40. val = p / MULTIPLIER
  41. if val.is_integer():
  42. split_values.append(int(val))
  43. else:
  44. split_values.append(round(val, 2))
  45.  
  46. # 输出结果
  47. print("拆分结果:")
  48. parts = []
  49. for v in split_values:
  50. if isinstance(v, int):
  51. parts.append(f"{v}×{MULTIPLIER}")
  52. else:
  53. parts.append(f"{v}×{MULTIPLIER}")
  54. print(" + ".join(parts) + f" = {TARGET_SUM}")
  55.  
  56. return split_values
  57.  
  58. def isclose(a, b):
  59. return abs(a - b) <= 1e-9
  60.  
  61. split_numbers()
  62.  
Success #stdin #stdout 0.04s 9372KB
stdin
Standard input is empty
stdout
拆分结果:
11.9×620 + 12.82×620 + 13.74×620 + 14.66×620 + 15.59×620 + 16.13×620 + 16.13×620 + 16.13×620 = 72602