Skip to content

Instantly share code, notes, and snippets.

@acras
Created March 28, 2013 01:58
Show Gist options
  • Save acras/5259884 to your computer and use it in GitHub Desktop.
Save acras/5259884 to your computer and use it in GitHub Desktop.
Resolve o dojo da pelissari 27/08/2013. Foi na força bruta mas é possível que exista uma fórmula.
#coding: utf-8
def run
print 'n: '
n = gets.to_i
print 'r: '
r = gets.to_i
raise 'n deve ser maior que 2' if n < 2
raise 'r deve ser maior que 1' if r <= 1
raise 'r deve ser menor ou igual a n' if r > n
#m será de 1 a n-1
best_m = -1
best_m_position = -1
1.upto(n-1).each do |m_to_try|
scenario = build_scenario(n, m_to_try)
position = evaluate_candidate(scenario, r)
puts "m=#{m_to_try} -> " + scenario.inspect + ' == ' + position.to_s
if position > best_m_position
best_m_position = position
best_m = m_to_try
end
end
puts "O melhor m é #{best_m} pois coloca a subestação na posição: #{best_m_position}"
end
def build_scenario(n,m)
a = []
1.upto(n) {|i| a << i}
b = a.cycle
r = [b.next] #tem que começar com 1
1.upto(n-1) do
next_number = -1
1.upto(m) { next_number = b.next }
r << next_number
end
r
end
def evaluate_candidate(arr, r)
res = -1
res = arr.index(r) + 1 if arr.count == arr.uniq.count
#o teste acima verifica se a solução é justa
res
end
run
@acras
Copy link
Author

acras commented Mar 28, 2013

a fórmula m=n-r+1 parece resolver boa parte das tuplas (n,r) mas com certeza não todas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment