fork download
  1. def split_number(total, parts, multiplier, max_product=10000):
  2. """
  3. 将数字分成指定份数,确保每份乘以特定值后不超过最大值
  4.  
  5. 参数:
  6. total: 93102
  7. parts: 10
  8. multiplier:590
  9. max_product: 乘积的最大值,默认为10000
  10.  
  11. 返回:
  12. 拆分后的数字列表,如果无法拆分则返回None
  13. """
  14. # 计算每个部分的最大值
  15. max_part = max_product // multiplier
  16.  
  17. # 检查是否可能拆分
  18. if total < parts or total > parts * max_part:
  19. print(f"无法将{total}分成{parts}份,使得每份乘以{multiplier}后不超过{max_product}")
  20. return None
  21.  
  22. # 初始化每个部分为最小值1
  23. result = [1] * parts
  24. remaining = total - parts # 减去已分配的最小值
  25.  
  26. # 分配剩余值
  27. i = 0
  28. while remaining > 0 and i < parts:
  29. # 计算当前位置最多可以分配多少
  30. add = min(remaining, max_part - 1) # 减1是因为已经有了1
  31. result[i] += add
  32. remaining -= add
  33. i += 1
  34.  
  35. # 验证结果
  36. if sum(result) != total:
  37. print("拆分失败")
  38. return None
  39.  
  40. # 打印拆分结果和乘积
  41. print(f"将{total}分成{parts}份,每份乘以{multiplier}后不超过{max_product}:")
  42. products = [num * multiplier for num in result]
  43. for i, (num, product) in enumerate(zip(result, products), 1):
  44. print(f"第{i}份: {num} × {multiplier} = {product}")
  45.  
  46. # 打印总和验证
  47. print(f"\n总和验证: {total} = {total}")
  48. print(f"乘积总和: {sum(products)} = {' + '.join(f'{num}×{multiplier}' for num in result)}")
  49.  
  50. return result
  51.  
  52. # 可以根据需要修改这些参数
  53. total_number = 93102 # 要拆分的总数字
  54. num_parts = 10 # 要分成的份数
  55. specific_number =590 # 要相乘的特定数字
  56. max_allowed = 10000 # 最大允许的乘积
  57.  
  58. # 执行拆分
  59. split_result = split_number(total_number, num_parts, specific_number, max_allowed)
  60.  
Success #stdin #stdout 0.02s 9292KB
stdin
Standard input is empty
stdout
无法将93102分成10份,使得每份乘以590后不超过10000