python 程序报错(分数背包问题)
python program give error(fraction knapsack problem)
python 的分数背包问题它在我 运行 代码时给出错误,是拆分函数不适用于整数值。
Traceback (most recent call last):
File "C:/Users/Akshay/Desktop/python/kapsack_problem.py", line 49, in <module>
.format(n)).split()
File "<string>", line 1
60 100 120
^
SyntaxError: invalid syntax
这里是 Python 程序的源代码,使用贪心算法解决小数背包问题。我做错了什么请告诉我。
提前致谢。
def fractional_knapsack(value, weight, capacity):
index = list(range(len(value)))
# contains ratios of values to weight
ratio = [v/w for v, w in zip(value, weight)]
# index is sorted according to value-to-weight ratio in decreasing order
index.sort(key=lambda i: ratio[i], reverse=True)
max_value = 0
fractions = [0]*len(value)
for i in index:
if weight[i] <= capacity:
fractions[i] = 1
max_value += value[i]
capacity -= weight[i]
else:
fractions[i] = capacity/weight[i]
max_value += value[i]*capacity/weight[i]
break
return max_value, fractions
n = int(input('Enter number of items: '))
value = input('Enter the values of the {} item(s) in order: '
.format(n)).split()
value = [int(v) for v in value]
weight = input('Enter the positive weights of the {} item(s) in order: '
.format(n)).split()
weight = [int(w) for w in weight]
capacity = int(input('Enter maximum weight: '))
max_value, fractions = fractional_knapsack(value, weight, capacity)
print('The maximum value of items that can be carried:', max_value)
print('The fractions in which the items should be taken:', fractions)
您似乎尝试使用 Python 2.x
解释器 运行 这段代码,而您的代码是用 Python 3
编写的。为了能够 运行 它,您需要检查您的计算机上是否安装了 Python 3
(有关安装说明,请参阅 here)。
给运行吧,运行
python3 my_script.py
在终端中。
另一种可能性是粘贴
#!/usr/bin/env python3
在您的 python 脚本的顶部。然后,如果您使文件可执行(例如,通过 运行ning chmod +x myscript.py
on ubuntu),您可以 运行 只需使用
./my_script.py
python 的分数背包问题它在我 运行 代码时给出错误,是拆分函数不适用于整数值。
Traceback (most recent call last):
File "C:/Users/Akshay/Desktop/python/kapsack_problem.py", line 49, in <module>
.format(n)).split()
File "<string>", line 1
60 100 120
^
SyntaxError: invalid syntax
这里是 Python 程序的源代码,使用贪心算法解决小数背包问题。我做错了什么请告诉我。 提前致谢。
def fractional_knapsack(value, weight, capacity):
index = list(range(len(value)))
# contains ratios of values to weight
ratio = [v/w for v, w in zip(value, weight)]
# index is sorted according to value-to-weight ratio in decreasing order
index.sort(key=lambda i: ratio[i], reverse=True)
max_value = 0
fractions = [0]*len(value)
for i in index:
if weight[i] <= capacity:
fractions[i] = 1
max_value += value[i]
capacity -= weight[i]
else:
fractions[i] = capacity/weight[i]
max_value += value[i]*capacity/weight[i]
break
return max_value, fractions
n = int(input('Enter number of items: '))
value = input('Enter the values of the {} item(s) in order: '
.format(n)).split()
value = [int(v) for v in value]
weight = input('Enter the positive weights of the {} item(s) in order: '
.format(n)).split()
weight = [int(w) for w in weight]
capacity = int(input('Enter maximum weight: '))
max_value, fractions = fractional_knapsack(value, weight, capacity)
print('The maximum value of items that can be carried:', max_value)
print('The fractions in which the items should be taken:', fractions)
您似乎尝试使用 Python 2.x
解释器 运行 这段代码,而您的代码是用 Python 3
编写的。为了能够 运行 它,您需要检查您的计算机上是否安装了 Python 3
(有关安装说明,请参阅 here)。
给运行吧,运行
python3 my_script.py
在终端中。
另一种可能性是粘贴
#!/usr/bin/env python3
在您的 python 脚本的顶部。然后,如果您使文件可执行(例如,通过 运行ning chmod +x myscript.py
on ubuntu),您可以 运行 只需使用
./my_script.py