2
I’m trying to solve the problem 1030 - Flavious Josephus Legend, from a challenge site.
I send the code and says it has the Runtime error (Accesses an invalid memory or array has little space):
vezes = gets.chomp().to_i
vezes.times do |num|
def jose(n,k) #Fiz o método de maneira correta?
if (n==0)
resultado=0
else
return resultado = (( jose(n-1,k)+ k - 1 ) % n)+1 #Eu entendo recursividade, porém acho que fiz errado aqui.
end
end
phrase=gets.split(" ") #Erro pode estar aqui, não sei fazer de outra maneira que o site aceita a leitura
n=phrase[0].to_i
k=phrase[1].to_i
puts "Case " + (num+1).to_s + ": " + jose(n,k).to_s
end
There is another way to do the exercise that the site accepts?
I made a comment up there on my code and also what does each_slice(2) mean? I know what "each" is, but not "Slice"...
– Misaee 21
@Misaee21 saw her edition - as for her, two things: wrong is a strong word - no, it’s not wrong, but is it necessary? : ) and avoid editing questions already answered to include new questions; there is not exactly a "follow up" of questions on Sopt, except when creating a new one - to clarify this and other simpler questions, I suggest you visit the chat.
– Daniel
@Misaee21 the
each.slice(n)
return to youn
elements rather than just one, as in the case ofeach
.– Daniel
https://i.etsystatic.com/12780725/d/il/3f8d4e/1369912852/il_340x270.1369912852_teyg.jpg
– Bacco