给定n个物品的重量和价值,任务是将这些物品放入容量为W的背包中以获得背包中的最大总值,我们可以重复放置相同的物品,也可以放置物品的一小部分。
例子:
Input: val[] = {14, 27, 44, 19}, wt[] = {6, 7, 9, 8}, W = 50
Output: 244.444
Input: val[] = {100, 60, 120}, wt[] = {20, 10, 30}, W = 50
Output: 300
方法:这里的想法是找到具有最大价值重量比的项目。然后只用这件物品装满整个背包,以最大化背包的最终价值。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum required value
float knapSack(int W, float wt[], float val[], int n)
{
// maxratio will store the maximum value to weight
// ratio we can have for any item and maxindex
// will store the index of that element
float maxratio = INT_MIN;
int maxindex = 0;
// Find the maximum ratio
for (int i = 0; i < n; i++) {
if ((val[i] / wt[i]) > maxratio) {
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
// The item with the maximum value to
// weight ratio will be put into
// the knapsack repeatedly until full
return (W * maxratio);
}
// Driver code
int main()
{
float val[] = { 14, 27, 44, 19 };
float wt[] = { 6, 7, 9, 8 };
int n = sizeof(val) / sizeof(val[0]);
int W = 50;
cout << knapSack(W, wt, val, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum required value
static float knapSack(int W, float wt[],
float val[], int n)
{
// maxratio will store the maximum value to weight
// ratio we can have for any item and maxindex
// will store the index of that element
float maxratio = Integer.MIN_VALUE;
int maxindex = 0;
// Find the maximum ratio
for (int i = 0; i < n; i++)
{
if ((val[i] / wt[i]) > maxratio)
{
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
// The item with the maximum value to
// weight ratio will be put into
// the knapsack repeatedly until full
return (W * maxratio);
}
// Driver code
public static void main(String[] args)
{
float val[] = {14, 27, 44, 19};
float wt[] = {6, 7, 9, 8};
int n = val.length;
int W = 50;
System.out.println(knapSack(W, wt, val, n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of the approach
import sys
# Function to return the maximum required value
def knapSack(W, wt, val, n):
# maxratio will store the maximum value to weight
# ratio we can have for any item and maxindex
# will store the index of that element
maxratio = -sys.maxsize-1;
maxindex = 0;
# Find the maximum ratio
for i in range(n):
if ((val[i] / wt[i]) > maxratio):
maxratio = (val[i] / wt[i]);
maxindex = i;
# The item with the maximum value to
# weight ratio will be put into
# the knapsack repeatedly until full
return (W * maxratio);
# Driver code
val = [ 14, 27, 44, 19 ];
wt = [ 6, 7, 9, 8 ];
n = len(val);
W = 50;
print(knapSack(W, wt, val, n));
# This code is contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum required value
static float knapSack(int W, float []wt,
float []val, int n)
{
// maxratio will store the maximum value to weight
// ratio we can have for any item and maxindex
// will store the index of that element
float maxratio = int.MinValue;
int maxindex = 0;
// Find the maximum ratio
for (int i = 0; i < n; i++)
{
if ((val[i] / wt[i]) > maxratio)
{
maxratio = (val[i] / wt[i]);
maxindex = i;
}
}
// The item with the maximum value to
// weight ratio will be put into
// the knapsack repeatedly until full
return (W * maxratio);
}
// Driver code
public static void Main()
{
float []val = {14, 27, 44, 19};
float []wt = {6, 7, 9, 8};
int n = val.Length;
int W = 50;
Console.WriteLine(knapSack(W, wt, val, n));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
244.444
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。