I am just starting ruby. So, thought to try out some problems posted online. (here is the problem on codechef) I wrote the following piece of code:
def h(n)
if n==0
value = 0
开发者_StackOverflow中文版 else
c_n = $number[n-1].to_i
value = ( h(n-1) * $A + c_n ) % $B
end
value
end
n, $A, $B, G = gets.scan(/\d+/).map! { |x| x.to_i }
(1..10**n).to_a.each {|x|
$number = x.to_s.rjust(n, '0')
if ( G == h(n) )
puts $number
break
end
}
I compiled it on windows with the input values: 3 11 111 92
The output on windows was as expected: 084 But, on the linux machine it is: 032
What can be the possible reason? Is there anything in the given piece of code which will make the difference?
Thanks to Nakilon's comment. I got the answer.
"string"[1] = "t"
in ruby 1.9
but in ruby "string"[1] = 116
So, in ruby 1.9:
c_n = $number[n-1].to_i
needs to be changed for ruby 1.8 as:
c_n = $number.split("")[n-1].to_i
精彩评论