- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我要解决的问题:
"The three witches in Hamlet can brew any potion provided they have the right ingredients. Suppose that five ingredients are necessary in making a health potion: eye of newt (eon), toe of frog (tof), wool of bat (wob), adder’s fork (af), and tooth of wolf (tow). Four reactions can >occur between these ingredients:
4 eon + 2 wob = 3 af + 4 tow
3 tow + 1 tof = 2 eon
1 wob + 2 af = 1 tof
4 tof + 7 tow + 2 af = 1 health potionAssuming you can control the order of reactions, write a program that can calculate the maximum number of health potions one can brew with a given amount of ingredients. Here is example output: If I have 34 eon, 59 tof, 20 wob, 5 af, and 20 tow, I can make seven health potions.”
Excerpt From: Ophir Frieder, Gideon Frieder, and David Grossman. “Computer Science Programming Basics with Ruby.” iBooks.
这是我的解决方案:
ingredients = Hash.new
potion = 0
puts "Welcome to potion brewer! To make a health potion you must combine 4 TOF + 7 TOW + 2 AF. Let's get started.\n\n"
puts "How many EON do you have?"
ingredients["EON"] = gets.to_i
puts "How many TOF do you have?"
ingredients["TOF"] = gets.to_i
puts "How many WOB do you have?"
ingredients["WOB"] = gets.to_i
puts "How many AF do you have?"
ingredients["AF"] = gets.to_i
puts "How many TOW do you have?"
ingredients["TOW"] = gets.to_i
while (ingredients["EON"] >= 4 and ingredients["WOB"] >= 2)
ingredients["AF"] += 3
ingredients["TOW"] += 4
ingredients["EON"] -= 4
ingredients["WOB"] -= 2
# ==/== DEBUG ==/==
# puts "4 EON and 2 WOB convereted into +3 AF and +4 TOW."
# puts ingredients["EON"]
# puts ingredients["WOB"]
end
while ((ingredients["TOF"]/4) < (ingredients["AF"]/2))
## puts "debug"
if (ingredients["WOB"] >= 1 and ingredients["AF"] >= 2)
ingredients["TOF"] += 1
ingredients["WOB"] -= 1
ingredients["AF"] -= 2
# puts "1 WOB and 2 AF converted to +1 TOF."
else
break
end
end
while (ingredients["TOF"] >= 4 and ingredients["TOW"] >= 7 and ingredients["AF"] >= 2)
potion += 1
ingredients["TOF"] -= 4
ingredients["TOW"] -= 7
ingredients["AF"] -= 2
# ==/== DEBUG ==/==
#puts "Potion created.."
end
puts "\n\nMade #{potion} potion(s).\n\n"
for name in ingredients.keys
puts "You have " + ingredients[name].to_s + " " + name + " left.\n"
end
无论如何,这是我能想到的“最整洁”的解决方法。我认为我正确地对转换进行了排序,这样在制作药水时就不会出现任何低效情况……并且我通过书中示例的输入得到了想要的结果。
任何人都可以确认它实际上看起来没问题/我没有错过一些可以进一步最大化我的药水的重大优化吗?我找不到与第三次转换 (1wob+2af=1tof) 有多大关系。
谢谢!
最佳答案
有趣的问题!
因此让我们重新表述一下:目标是计算“健康”药水,如果此药水的任何成分缺失,则找到可用于制造缺失成分的其他药水。
这听起来像是一种递归算法。因此,首先,让我们对“制作药水”问题进行建模。
假设 e 有一个药水公式,一个包含所有需要的成分(负值)的散列和生成的成分,正值。
例如:
4 eon + 2 wob = 3 af + 4 tow
可以写成:
formulae={:eon=>-4,:wob=>-2,:af=>3,:tow=>4}
因此,计算公式将非常简单:
def compute_formulae ingredients,formulae
result=ingredients.clone
formulae.each do |needed,amount|
if ingredients[needed]<-amount
puts "Missing #{needed}" # The is an ingredient missing, we should probably exit now
return nil
else
result[needed]+=amount
end
end
result
end
现在的问题是缺少配料怎么办?我们必须根据我们现有的成分,在公式列表中找到一个我们可以用来“创建它”的公式
formulas=[
{:tof=>-4,:tow=>-7,:af=>-2,:health=>1},
{:eon=>-4,:wob=>-2,:af=>3,:tow=>4},
{:tow=>-3,:tof=>-1,:eon=>2},
{:wob=>-1,:af =>-2,:tof=>1}
]
formulas.each{|f| f.default=0} # Just ensure that there is de fault value for all ingredients
def find_missing_ingredient ingredients,formulas,missing
formulas.each do | formulae |
if formulae[missing]>0
compute_formulae_ingredient ingredients,formulae
end
end
end
# so basically, the problem is
ingredients={:eon=>34,:tof=>59,:wob=>20,:af=>5,:tow=>20}
ingredients.default=0
while find_missing_ingredient ingredients,formulas,:health
end
现在,有一些小细节,比如主循环(我们需要继续,只要我们能得到新的“健康”,错误(在这个递归循环中什么时候停止),输入部分,但我留下了这个给读者!
关于ruby - 使用 ruby 中的成分和公式转换优化药水酿造程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440997/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!