fork download
  1. # ====================== 配置区 ======================
  2. total = 134.8
  3. price = 64.5
  4. # ====================================================
  5.  
  6. import random
  7. random.seed(1) # 结果永远固定
  8.  
  9. max_single = 1000 / price
  10. count = int(total / max_single) + 1
  11.  
  12. # 生成不重复、合法数字
  13. nums = []
  14. base = round(total / count, 2)
  15. for i in range(count):
  16. val = round(base + (i - count//2) * 0.02, 2)
  17. while not (val * price * 100).is_integer():
  18. val = round(val + 0.01, 2)
  19. # 强制不重复
  20. while val in nums:
  21. val = round(val + 0.01, 2)
  22. while not (val * price * 100).is_integer():
  23. val = round(val + 0.01, 2)
  24. nums.append(val)
  25.  
  26. random.shuffle(nums)
  27.  
  28. # 精准补总和,绝对不重复
  29. diff = round(total - sum(nums), 2)
  30. step = 0.01
  31. max_attempts = 5000
  32. i = 0
  33.  
  34. while abs(diff) > 0.001 and i < max_attempts:
  35. idx = random.randint(0, count-1)
  36. current = nums[idx]
  37. new_val = round(current + step if diff>0 else current - step, 2)
  38.  
  39. # 规则1:两位小数乘积
  40. if not (new_val * price * 100).is_integer():
  41. i += 1
  42. continue
  43.  
  44. # 规则2:绝对不重复
  45. temp = nums.copy()
  46. temp.remove(current)
  47. if new_val not in temp and new_val > 0:
  48. nums[idx] = new_val
  49. diff = round(total - sum(nums), 2)
  50. i += 1
  51.  
  52. # 最后全局强制去重(终极保险)
  53. final = []
  54. for num in nums:
  55. while num in final:
  56. num = round(num + 0.01, 2)
  57. while not (num * price * 100).is_integer():
  58. num = round(num + 0.01, 2)
  59. final.append(num)
  60.  
  61. # 重新校准总和
  62. s = sum(final)
  63. if abs(total - s) > 0.001:
  64. final[0] = round(final[0] + (total - s), 2)
  65.  
  66. # ====================== 输出纯数字 ======================
  67. for n in final:
  68. print(n)
  69.  
  70. print("\n总和校验:", round(sum(final), 2))
  71. # ======================================================
Success #stdin #stdout 0.04s 11616KB
stdin
Standard input is empty
stdout
14.98
15.02
15.04
14.98
14.96
14.9
15.06
14.92
14.94

总和校验: 134.8