Created
May 5, 2013 11:00
-
-
Save alucky0707/5520496 to your computer and use it in GitHub Desktop.
AOJ 110
This file contains hidden or 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
while s = gets | |
break if s.strip == "" | |
_ = s.match /([0-9X]+)\+([0-9X]+)\=([0-9X]+)/ | |
a = _[1] | |
b = _[2] | |
c = _[3] | |
answer = nil | |
(0..9).each do|i| | |
next if i == 0 && (a != 'X' && a[0] == 'X' || | |
b != 'X' && b[0] == 'X' || | |
c != 'X' && c[0] == 'X') | |
x = a.gsub(/X/, i.to_s).to_i | |
y = b.gsub(/X/, i.to_s).to_i | |
z = c.gsub(/X/, i.to_s).to_i | |
if x + y == z | |
answer = i | |
break | |
end | |
end | |
puts answer ? answer.to_s : 'NA' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment