- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我发现此代码使用蛮力机制解决背包问题(这主要是为了学习,因此无需指出动态更有效)。我得到了可以工作的代码,并且了解了大部分代码。最多。这是问题:
我注意到这两个条件,我不知道它们如何工作以及为什么在代码中-我知道它们至关重要,因为我进行的任何更改都会导致算法产生错误的结果:
// if bit not included then skip
if (((i >> j) & 1) != 1) continue;
// if bit match then add
if (((bestPosition >> j) & 1) == 1)
{
include.Add(Items[j]);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KnapSack2
{
class BruteForce
{
public double Capacity { get; set; }
public Item[] Items { get; set; }
public Data Run()
{
int bestValue = 0;
int bestPosition = 0;
int size = Items.Length;
var permutations = (long)Math.Pow(2,size);
for (int i = 0; i<permutations; i++)
{
int total = 0;
int weight = 0;
for (int j = 0; j<size; j++)
{
//jeżeli bit "not included" to omin"
if(((i>>j)&1)!=1)
continue;
total += Items[j].v;
weight += Items[j].w;
}
if (weight <= Capacity && total>bestValue)
{
bestPosition = i;
bestValue = total;
}
}
var include = new List<Item>();
for (int j = 0; j<size; j++)
{
// jeżeli bit pasuje, wtedy dodaj
if (((bestPosition>>j) & 1)==1)
include.Add(Items[j]);
}
return new Data { BestValue = bestValue, Include = include };
}//End of Run
}
}
var ks = new BruteForce
{
Capacity = MaxWeight,
Items = items.ToArray()
};
Data result = ks.Run();
最佳答案
此&
是bitwise-AND
The bitwise-AND operator compares each bit of its first operand to the corresponding bit of its second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
>>
是右移运算符
The right-shift operator (>>) shifts its first operand right by the number of bits specified by its second operand.
if (((i >> j) & 1) != 1) continue;
i >> j
将
i
的位置右移
j
的位置。
int number = 5;
number
的二进制表示为:
0000 0000 0000 0000 0000 0000 0000 0101
int shiftNumbersBitByOne = a >> 1;
shiftNumbersBitByOne
的二进制表示为:
0000 0000 0000 0000 0000 0000 0000 0010
What does exactly this operator ?
a
和
b
,那么
a&b
的结果如下:
a = 0001 0100 1010 0001 1000 1010 1101 0011
b = 0010 1100 1111 0111 0011 1010 1011 0111
a & b = 0000 0100 1010 0001 0000 1010 1001 0011
(i >> j) & 1
中,我们在
i >> j
的结果与1的二进制表示形式之间应用按位与运算符
0000 0000 0000 0000 0000 0000 0000 0001
When the result of
(i >> j) & 1
would be 1?
i >> j
的最后一位为1时,才会发生这种情况。
The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.
// This is the total weight limit.
public double Capacity { get; set; }
// This is an array with all the given items.
public Item[] Items { get; set; }
item.v
和
item.w
进行访问。我建议您将其分别重命名为值和权重,以使代码更有意义。
int size = Items.Length;
是可用项目的数量。
var permutations = (long)Math.Pow(2,size);
permutations
?
permutations
代表什么?
for (int i = 0; i<permutations; i++)
{
// ...
}
for (int j = 0; j < size; j++)
{
// Here you check if the item in position j
// is included in the current combination.
// If it is not, you go to the next value of the loop's variable
// and you make the same check.
if(((i>>j)&1)!=1)
continue;
// The j-th item is included in the current combination.
// Hence we add it's
// value to the total value accumulator and it's weight to
// the total weight accumulator.
total += Items[j].v;
weight += Items[j].w;
}
weight
小于极限值
和,则总值大于当前的最佳总值,我们将此组合作为当前的最佳值:
bestPosition = i;
bestValue = total;
// The include is a list that will hold the items of the best combination.
var include = new List<Item>();
// We loop through all the available items
for (int j = 0; j<size; j++)
{
// If the items is included in the best combination,
// add this item to the include list.
if (((bestPosition>>j) & 1)==1)
include.Add(Items[j]);
}
关于c# - 背包-蛮力算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669259/
我是一名优秀的程序员,十分优秀!