Created
March 3, 2018 08:26
-
-
Save maehrm/0a39218e3452ae2dec72641e17fb40b1 to your computer and use it in GitHub Desktop.
数式 | Aizu Online Judge http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0041
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solv arr | |
arr.permutation.each {|a| | |
['+', '-', '*'].repeated_permutation(3).each {|op| | |
exp1 = "(((#{a[0]} #{op[0]} #{a[1]}) #{op[1]} #{a[2]}) #{op[2]} #{a[3]})" | |
return exp1 if eval(exp1) == 10 | |
exp2 = "((#{a[0]} #{op[0]} #{a[1]}) #{op[1]} (#{a[2]} #{op[2]} #{a[3]}))" | |
return exp2 if eval(exp2) == 10 | |
exp3 = "((#{a[0]} #{op[0]} (#{a[1]} #{op[1]} #{a[2]})) #{op[2]} #{a[3]})" | |
return exp3 if eval(exp3) == 10 | |
} | |
} | |
0 | |
end | |
while str = gets | |
arr = str.split.map(&:to_i) | |
break if arr.reduce(:+).zero? | |
puts solv arr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment