Created
January 8, 2018 19:12
-
-
Save theist/5d83340a5c9be1a8c6e0f07523ba87d5 to your computer and use it in GitHub Desktop.
implementacion rapida ruby de resolucion de "adosados" metodo ABN
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
#!/usr/bin/env ruby | |
# | |
numero = ARGV.shift.to_i | |
# si el numero es menor de 100 o mayor de 999 no puedo! | |
puts "+-----------------+" | |
puts "|" + "#{numero}".center(17) + "|" | |
puts "+-----+-----+-----+" | |
centenas = numero / 100 | |
decenas = (numero % 100) / 10 | |
unidades = (numero % 10) | |
puts "|" + "#{centenas}".center(5) + "|" + "#{decenas}".center(5) + "|" + "#{unidades}".center(5) + "| => [" + "#{centenas * 100} + #{decenas * 10} + #{unidades}]" | |
for piso in (1..10) do | |
if rand(10) > 5 | |
if centenas > 0 | |
centenas = centenas - 1 | |
decenas = decenas + 10 | |
else | |
next | |
end | |
else | |
if decenas > 0 | |
decenas = decenas - 1 | |
unidades = unidades + 10 | |
else | |
next | |
end | |
end | |
puts "|" + "#{centenas}".center(5) + "|" + "#{decenas}".center(5) + "|" + "#{unidades}".center(5) + "| => [" + "#{centenas * 100} + #{decenas * 10} + #{unidades}]" | |
end | |
puts "+-----+-----+-----+" |
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
~$ ruby adosados.rb 645 | |
+-----------------+ | |
| 645 | | |
+-----+-----+-----+ | |
| 6 | 4 | 5 | => [600 + 40 + 5] | |
| 6 | 3 | 15 | => [600 + 30 + 15] | |
| 6 | 2 | 25 | => [600 + 20 + 25] | |
| 6 | 1 | 35 | => [600 + 10 + 35] | |
| 5 | 11 | 35 | => [500 + 110 + 35] | |
| 5 | 10 | 45 | => [500 + 100 + 45] | |
| 5 | 9 | 55 | => [500 + 90 + 55] | |
| 5 | 8 | 65 | => [500 + 80 + 65] | |
| 4 | 18 | 65 | => [400 + 180 + 65] | |
| 3 | 28 | 65 | => [300 + 280 + 65] | |
| 3 | 27 | 75 | => [300 + 270 + 75] | |
+-----+-----+-----+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment