蟒蛇的宝石小偷

时间:2013-10-19 19:58:42

标签: python

  Bonnie和Clyde是两个决定抢劫珠宝的小偷   故事。每个人都有一个袋子,他们将用来携带他们的战利品,但每个袋子   在断裂之前只能容纳n克珠宝。他们是商店   抢劫有几种不同类型的珠宝,每种都有自己的珠宝   重量和货币价值。

     
      
  • 克莱德填充行李的方法是“贪婪”。这意味着他一次一个地将珠宝添加到他的包里,并选择哪一颗宝石   接下来,他总是选择最有价值的珠宝   不会让他超过重量限制。
  •   
  • 如果克莱德有两个价值相同但重量不同的珠宝之间的选择,他总会选择较轻的珠宝。
  •   
  • Bonnie认为,如果她对自己的包包装的方式更加聪明,她会经常离开珠宝店,获得更有价值的运输   比克莱德。你的任务是帮助表明这是真的。为此,   你会写一个程序,jewelthief.py,它将作为输入a   珠宝清单(每个都有一个以克为单位的重量和一个以美元计的价值)   和包的重量限制。然后你的程序将输出   使用贪婪选择时袋中珠宝的总价值   方法,以及该值与最大可能值的比较,   您将使用动态编程计算。输入将是   通过标准输入给出。
  •   
  • 第一行输入将包含一个整数,该整数对应于行李可以容纳的最大重量。
  •   
  • 以下每一行都包含两个整数,并将定义一个新的宝石类型。
  •   
  • 第一个整数将是以克为单位的宝石的重量,第二个整数将是该宝石的美元价值。
  •   
     

注意,您可以假设被盗的商店有无限制   每种宝石的数量。

     

示例输入1:

10
3 4
2 3
1 1
     

示例输出1:

The greedy approach would steal $13 of jewels
The optimal approach would steal $15 of jewels
     

示例输入2:

575
125 3000
50 100
500 6000
25 30
     

示例输出2:

The greedy approach would steal $6130 of jewels
The optimal approach would steal $12130 of jewels
     

示例输入3:

1500
151 150
150 150 
     

示例输出3:

The greedy approach would steal $1500 of jewels
The optimal approach would steal $1500 of jewels

我对python很新,这个问题让我很困惑。 我无法计算如何计算宝石价值,并加起来并停止两个小偷的最大重量。

到目前为止,这是我的代码:

bag=input('max_weight of bag: ')  #weight of the bag
Clyde_value=0     #initialize weight and value for both thieves
Clyde_weight=0
Bonnie_value=0
Bonnie_weight=0
while True:
    try:
        line=input()  #input weights and values at once
    except:
        break

    if len(line)==0:
        break

    a=list(map(int,line.split(None)))
    if len(line)<1:
        break
    lst=[]
    lst.append(a)

    #print(a)  #this will printout the list below when enter the  sample input 1   
    #[3, 4]
    #[2, 3]
    #[1, 1]
    weight_jewel=a[0]  #print this will give 3 2 1 for sample input 1
    value_jewel=a[1]  #print this will give  4 3 1 for sample input 1

while Clyde_weight <= bag:   #start calculating clyde's jewel value

我不太确定我的代码是否适合这个问题。请给我一些建议和提示,或者一个例子会很棒。非常感谢你。

0 个答案:

没有答案
相关问题