C++ C++;蛮力背包的一部分
读者, 好吧,我想我只是被他妈的有点傻了。 我在实现背包,我想我实现了蛮力算法,像以前一样1到2次。所以我决定再做一个。 这是我塞进的东西 让我们确定W是最大重量,W(min)是我们可以放入背包的最小加权元素,比如C++ C++;蛮力背包的一部分,c++,algorithm,brute-force,knapsack-problem,imperative-programming,C++,Algorithm,Brute Force,Knapsack Problem,Imperative Programming,读者, 好吧,我想我只是被他妈的有点傻了。 我在实现背包,我想我实现了蛮力算法,像以前一样1到2次。所以我决定再做一个。 这是我塞进的东西 让我们确定W是最大重量,W(min)是我们可以放入背包的最小加权元素,比如k=W/W(min)次。我解释这一点是因为读者,你们更清楚我为什么要问我的问题 现在。如果我们想象我们有三种东西可以放在背包里,我们的背包可以储存15个质量单位,让我们把每一个单位的重量分别计算为它的数量。所以我们可以放15件第一类的东西,或者7件第二类的东西和1件第一类的东西。但是,
k=W/W(min)
次。我解释这一点是因为读者,你们更清楚我为什么要问我的问题
现在。如果我们想象我们有三种东西可以放在背包里,我们的背包可以储存15个质量单位,让我们把每一个单位的重量分别计算为它的数量。所以我们可以放15件第一类的东西,或者7件第二类的东西和1件第一类的东西。但是,像2222221[7ed]
和12222222[7ed]
这样的组合对我们的意义相同。计算它们是浪费我们为决策付出的任何资源。(这是一个笑话,因为如果我们有一个更便宜的算法,bf就是一种浪费,但我非常感兴趣)
我猜我们需要通过所有可能的组合进行选择的类型被称为“重复组合”。C'(n,k)
的数量计为(n+k-1)/(n-1)!k代码>。
(当我输入我的信息时,我发现我的理论中有一个漏洞。我们可能需要添加一个空的、零加权的零定价项目来保留自由空间,它可能只是增加了n 1)
那么,怎么了
因为这个问题在这里被很好地描述了^我真的不想以这种方式使用堆栈,我想在单个循环中生成变化,从I=0到I,答案由Minoru在这里提供,
只增加第一个元素就足够了,然后我们计算所有进位,设置进位的位置,并计算重置值,作为要重置和重置的元素的最大值
这是我的密码:
#include <iostream>
using namespace std;
static long FactNaive(int n)
{
long r = 1;
for (int i = 2; i <= n; ++i)
r *= i;
return r;
}
static long long CrNK (long n, long k)
{
long long u, l;
u = FactNaive(n+k-1);
l = FactNaive(k)*FactNaive(n-1);
return u/l;
}
int main()
{
int numberOFchoices=7,kountOfElementsInCombination=4;
int arrayOfSingleCombination[kountOfElementsInCombination] = {0,0,0,0};
int leftmostResetPos = kountOfElementsInCombination;
int resetValue=1;
for (long long iterationCounter = 0; iterationCounter<CrNK(numberOFchoices,kountOfElementsInCombination); iterationCounter++)
{
leftmostResetPos = kountOfElementsInCombination;
if (iterationCounter!=0)
{
arrayOfSingleCombination[kountOfElementsInCombination-1]++;
for (int anotherIterationCounter=kountOfElementsInCombination-1; anotherIterationCounter>0; anotherIterationCounter--)
{
if(arrayOfSingleCombination[anotherIterationCounter]==numberOFchoices)
{
leftmostResetPos = anotherIterationCounter;
arrayOfSingleCombination[anotherIterationCounter-1]++;
}
}
}
if (leftmostResetPos != kountOfElementsInCombination)
{
resetValue = 1;
for (int j = 0; j < leftmostResetPos; j++)
{
if (arrayOfSingleCombination[j] > resetValue)
{
resetValue = arrayOfSingleCombination[j];
}
}
for (int j = leftmostResetPos; j != kountOfElementsInCombination; j++)
{
arrayOfSingleCombination[j] = resetValue;
}
}
for (int j = 0; j<kountOfElementsInCombination; j++)
{
cout<<arrayOfSingleCombination[j]<<" ";
}
cout<<"\n";
}
return 0;
}
#包括
使用名称空间std;
静态长FactNaive(int n)
{
长r=1;
对于(int i=2;i重置值)
{
resetValue=arrayOfSingleCombination[j];
}
}
for(int j=leftmostResetPos;j!=kountofelementsincomposition;j++)
{
arrayOfSingleCombination[j]=重置值;
}
}
对于(int j=0;jI无法计算出你在问什么我需要知道的是,如何在从零到NumberOfrcomb(n,k)的循环中生成rcomb(n,k)元素1。就像我们有n=4一样;(元素从0到3)k=3;我如何在没有函数调用它们自己的情况下,在一个循环中生成,比如100100110111,120121122123130131132133,等等。这个答案很接近,但我不明白他为什么需要排列和2**n。
#include <iostream>
using namespace std;
static long FactNaive(int n)
{
long r = 1;
for (int i = 2; i <= n; ++i)
r *= i;
return r;
}
static long long CrNK (long n, long k)
{
long long u, l;
u = FactNaive(n+k-1);
l = FactNaive(k)*FactNaive(n-1);
return u/l;
}
int main()
{
int numberOFchoices=7,kountOfElementsInCombination=4;
int arrayOfSingleCombination[kountOfElementsInCombination] = {0,0,0,0};
int leftmostResetPos = kountOfElementsInCombination;
int resetValue=1;
for (long long iterationCounter = 0; iterationCounter<CrNK(numberOFchoices,kountOfElementsInCombination); iterationCounter++)
{
leftmostResetPos = kountOfElementsInCombination;
if (iterationCounter!=0)
{
arrayOfSingleCombination[kountOfElementsInCombination-1]++;
for (int anotherIterationCounter=kountOfElementsInCombination-1; anotherIterationCounter>0; anotherIterationCounter--)
{
if(arrayOfSingleCombination[anotherIterationCounter]==numberOFchoices)
{
leftmostResetPos = anotherIterationCounter;
arrayOfSingleCombination[anotherIterationCounter-1]++;
}
}
}
if (leftmostResetPos != kountOfElementsInCombination)
{
resetValue = 1;
for (int j = 0; j < leftmostResetPos; j++)
{
if (arrayOfSingleCombination[j] > resetValue)
{
resetValue = arrayOfSingleCombination[j];
}
}
for (int j = leftmostResetPos; j != kountOfElementsInCombination; j++)
{
arrayOfSingleCombination[j] = resetValue;
}
}
for (int j = 0; j<kountOfElementsInCombination; j++)
{
cout<<arrayOfSingleCombination[j]<<" ";
}
cout<<"\n";
}
return 0;
}