贪婪法求助!
本帖最后由 旧城以西灬 于 2020-2-9 14:46 编辑大佬们能不能康康是哪里出了问题 你输入的方式不对,应该这样输入:
1000 5
电脑 200 20
收音机 20 4
钟 175 10
花瓶 50 2
书 10 1
小偷拿走了花瓶
小偷拿走了钟
小偷拿走了电脑
小偷拿走了书
小偷拿走了收音机
总价值: 455美元 发代码,不要只贴张图。 把代码发出来,不然不好看也不好改 zltzlt 发表于 2020-2-9 14:49
发代码,不要只贴张图。
"""
贪婪法:在对问题求解时,总是做出在当前看来是最好的选择,不追求最优解,快速找到满意解。
输入:
20 6
电脑 200 20
收音机 20 4
钟 175 10
花瓶 50 2
书 10 1
油画 90 9
"""
class Thing(object):
"""物品"""
def __init__(self, name, price, weight):
self.name = name
self.price = price
self.weight = weight
@property
def value(self):
"""价格重量比"""
return self.price / self.weight
def input_thing():
"""输入物品信息"""
name_str, price_str, weight_str = input().split()
return name_str, int(price_str), int(weight_str)
def main():
"""主函数"""
max_weight, num_of_things = map(int, input().split())
all_things = []
for _ in range(num_of_things):
all_things.append(Thing(*input_thing()))
all_things.sort(key=lambda x: x.value, reverse=True)
total_weight = 0
total_price = 0
for thing in all_things:
if total_weight + thing.weight <= max_weight:
print(f'小偷拿走了{thing.name}')
total_weight += thing.weight
total_price += thing.price
print(f'总价值: {total_price}美元')
if __name__ == '__main__':
main() 一个账号 发表于 2020-2-9 14:54
把代码发出来,不然不好看也不好改
"""
贪婪法:在对问题求解时,总是做出在当前看来是最好的选择,不追求最优解,快速找到满意解。
输入:
20 6
电脑 200 20
收音机 20 4
钟 175 10
花瓶 50 2
书 10 1
油画 90 9
"""
class Thing(object):
"""物品"""
def __init__(self, name, price, weight):
self.name = name
self.price = price
self.weight = weight
@property
def value(self):
"""价格重量比"""
return self.price / self.weight
def input_thing():
"""输入物品信息"""
name_str, price_str, weight_str = input().split()
return name_str, int(price_str), int(weight_str)
def main():
"""主函数"""
max_weight, num_of_things = map(int, input().split())
all_things = []
for _ in range(num_of_things):
all_things.append(Thing(*input_thing()))
all_things.sort(key=lambda x: x.value, reverse=True)
total_weight = 0
total_price = 0
for thing in all_things:
if total_weight + thing.weight <= max_weight:
print(f'小偷拿走了{thing.name}')
total_weight += thing.weight
total_price += thing.price
print(f'总价值: {total_price}美元')
if __name__ == '__main__':
main() 旧城以西灬 发表于 2020-2-9 16:03
"""
贪婪法:在对问题求解时,总是做出在当前看来是最好的选择,不追求最优解,快速找到满意解。
输入 ...
代码用 "<>" 贴起来 一个账号 发表于 2020-2-9 16:06
代码用 "" 贴起来
class Thing(object):
"""物品"""
def __init__(self, name, price, weight):
self.name = name
self.price = price
self.weight = weight
@property
def value(self):
"""价格重量比"""
return self.price / self.weight
def input_thing():
"""输入物品信息"""
name_str, price_str, weight_str = input().split()
return name_str, int(price_str), int(weight_str)
def main():
"""主函数"""
max_weight, num_of_things = map(int, input().split())
all_things = []
for _ in range(num_of_things):
all_things.append(Thing(*input_thing()))
all_things.sort(key=lambda x: x.value, reverse=True)
total_weight = 0
total_price = 0
for thing in all_things:
if total_weight + thing.weight <= max_weight:
print(f'小偷拿走了{thing.name}')
total_weight += thing.weight
total_price += thing.price
print(f'总价值: {total_price}美元')
if __name__ == '__main__':
main() zltzlt 发表于 2020-2-9 21:47
你输入的方式不对,应该这样输入:
没太懂,运行之后贴您的这个代码吗?贴上去还是不对哇 厉害呀,希望学完之后能和你们一样厉害!!! 旧城以西灬 发表于 2020-2-10 11:11
没太懂,运行之后贴您的这个代码吗?贴上去还是不对哇
不要一次输入啊,换行输入
先输
拿的物品重量拿的物品件数
再输
物品名称 物品价格 物品重量
页:
[1]