Guest User

Untitled

a guest
May 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #---
  2. # Author:: Ian Yang
  3. # Created:: <2010-05-24 16:18:23>
  4. #+++
  5. #
  6. require 'pp'
  7.  
  8. def optimal_choise(diff)
  9. optimal_value = 0
  10. optimal_index = 0
  11.  
  12. current_value = 0
  13. (1..6).each do |i|
  14. current_value = current_value + diff - (9 * i)
  15. if current_value > optimal_value
  16. optimal_index = i
  17. optimal_value = current_value
  18. end
  19. end
  20.  
  21. optimal_index
  22. end
  23.  
  24. M = 8
  25. N = 4
  26. E = [1, 2, 3, 4, 5, 6].reduce(:+) / 6.0
  27.  
  28. $expectations = Array.new(M + 1) {Array.new(N + 1, -1)}
  29. $choices = Array.new(M + 1) {Array.new(N + 1, -1)}
  30.  
  31. $expectations.each_with_index do |row, i|
  32. row[0] = i * E
  33. if i < row.length
  34. row[i] = 10 * i * E
  35. end
  36. end
  37. $choices.each_with_index do |row, i|
  38. row[0] = 6
  39. if i < row.length
  40. row[i] = 0
  41. end
  42. end
  43.  
  44. def calculate(m, n)
  45. calculate(m - 1, n) if $expectations[m - 1][n] < 0
  46. calculate(m - 1, n - 1) if $expectations[m - 1][n - 1] < 0
  47.  
  48. diff = $expectations[m - 1][n] - $expectations[m - 1][n - 1]
  49. $choices[m][n] = optimal_choise(diff)
  50.  
  51. $expectations[m][n] = 0
  52. if $choices[m][n] > 0
  53. $expectations[m][n] += $choices[m][n] * diff
  54. $expectations[m][n] -= 9 * ($choices[m][n] * ($choices[m][n] + 1)) / 2
  55. end
  56. $expectations[m][n] /= 6.0
  57. $expectations[m][n] += 35 + $expectations[m - 1][n - 1]
  58. end
  59.  
  60. calculate(8, 4)
  61.  
  62. def get_result(*sequence)
  63. sequence = sequence.flatten.compact
  64. m = 8
  65. n = 4
  66.  
  67. result = Array.new(4, 0)
  68. sequence.each do |i|
  69. if i <= $choices[m][n]
  70. result[m - n - 1] += i
  71. else
  72. result[n - 1] += i * 10
  73. n -= 1
  74. end
  75. m -= 1
  76. end
  77. result
  78. end
  79.  
  80. pp $expectations
  81. pp $choices
  82.  
  83. sum = 0
  84. 10000.times do |i|
  85. input = []
  86. 8.times { input << (rand(6) + 1) }
  87. r = get_result(input)
  88. if i % 1000 == 0
  89. p ">>> #{i} <<<"
  90. p input
  91. p r
  92. end
  93. sum += r.inject(:+)
  94. end
  95.  
  96. p sum / 10000.0
Add Comment
Please, Sign In to add comment