当前位置:网站首页>Knapsack problem unrelated to profit (depth first search)
Knapsack problem unrelated to profit (depth first search)
2022-07-07 05:09:00 【Madness makes freedom】
/**< 1th example) */
/** \brief
* Choose from nine items 3 Make its weight equal to 500 The absolute value of the difference between grams is the smallest
* \ Find the object number
* \param
* \return
*
*/
/**< Loop solution , Loop solving can only deal with selecting a specific number of items , Not universal ; */
/**< 1th example) */
/** \brief
* Choose from nine items 3 Make its weight equal to 500 The absolute value of the difference between grams is the smallest
* \ Find the object number
* \param
* \return
*
*/
/**< Loop solution */
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n=9,abs_cha=1000;
int matr[n+1];
cout << " Input " << n << " The weight of an item :\n";
for(int i=1;i<=n;++i)
cin >> matr[i];
int besti,bestj,bestk;
int bestw,temp_sum=0;
for(int i=1;i<=7;++i)
for(int j=i;j<=8;++j)
for(int k=j;k<=9;++k)
{
temp_sum=matr[i]+matr[j]+matr[k];
if(abs(temp_sum-500)<abs_cha)
{
bestw=temp_sum;
abs_cha=abs(temp_sum-500);
besti=i;
bestj=j;
bestk=k;
}
}
printf("choose index is : %d %d %d \n",besti,bestj,bestk);
printf("best weight is:%d",bestw);
return 0;
}
/** \brief
* stay n Choose from items k Make its weight equal to 500 The absolute value of the difference between grams is the smallest
* \ Find the object number
* \param
* \return
*
*/
/**< Depth first search solution */
/** \brief
* stay n Choose from items k Make its weight equal to 500 The absolute value of the difference between grams is the smallest
* \ Find the object number
* \param
* \return
*
*/
/**< Depth first search solution */
/**
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> ans,temp;
int bestw=0,abs_cha=1000;
int matr[10];
void DFS(int index,int sum,int weight);
int main()
{
int n,sum;
cout << " Enter the total number of items and the number of items to select :\n";
cin >> n >> sum;
cout << " Input " << n << " The weight of an item :\n";
for(int i=1;i<=n;++i)
cin >> matr[i];
DFS(1,0,0);
printf("choose index is : \n");
for(size_t i=0;i<ans.size();++i)
cout << ans[i] << ' ';
cout << endl;
printf("best weight is:%d",bestw);
return 0;
}
void DFS(int index,int sum,int weight)
{
if(index>9||sum>3||abs_cha<weight-500)//< Absolute value cannot be added here
return;
if(sum==3)
{
if(abs_cha>abs(weight-500))
{
ans=temp;
bestw=weight;
abs_cha=abs(weight-500);
}
return;
}
temp.push_back(index);
if(weight+matr[index]-500<abs_cha) // This will save a little time , Not every item should be chosen
DFS(index+1,sum+1,weight+matr[index]); // because abs_cha It must be a non negative number , When weight+matr[index]-500
temp.pop_back(); // Than abs_cha When I was young , Then this combination must not be the best choice
DFS(index+1,sum,weight);
}
*/
/**< 2th example */
/** \brief
* There is a backpack that can hold items weighing S, existing n Item ,
* \ Ask if it's possible to n Select several items from the items and put them into the backpack , Make it put in
* \ The sum of the weights is just S
* \return
*
*/
/** \brief Recursive design analysis
* Knap(s,n) Indicates that the capacity of the backpack is still left s, also n Items are not selected
* \
* \param
* \return
*
*/
This recursive function is quite good , Output subscripts directly in recursive functions , You can learn . This is seen in the textbook of algorithm design and analysis .
/**< 2th example */
/** \brief
* There is a backpack that can hold items weighing S, existing n Item ,
* \ Ask if it's possible to n Select several items from the items and put them into the backpack , Make it put in
* \ The sum of the weights is just S
* \return
*
*/
/** \brief Recursive design analysis
* Knap(s,n) Indicates that the capacity of the backpack is still left s, also n Items are not selected
* \
* \param
* \return
*
*/
#include <iostream>
using namespace std;
const int maxn=10;
int matr[maxn];
bool Knap(double rest,int unselected );
int main()
{
int n;
double s;
cout << " Enter the number of items and backpack capacity :\n";
cin >> n >> s;
cout << " Input " << n<< " The weight of an item :\n";
for(int i=1;i<=n;++i)
cin >> matr[i];
if(Knap(s,n)==false)
cout << "No solution\n";
return 0;
}
bool Knap(double rest,int unselected )
{
if(rest==0)
return true;
else if(rest<0||(rest>0&&unselected<1))
return false;
else if(Knap(rest-matr[unselected],unselected-1)==true) // Select the first unselected Item
{
cout << "choose : " << unselected << "th" << endl;
return true;
}
else
return Knap(rest,unselected-1);// No choice unselected Item
}
/**< 2th example */
/** \brief
* There is a backpack that can hold items weighing S, existing n Item ,
* \ Ask if it's possible to n Select several items from the items and put them into the backpack , Make it put in
* \ The sum of the weights is just S
* \return
*
*/
/** \brief Depth first search analysis
* Knap(index,sum_value) Indicates that at this time, it is right for index Select items , Selected items
* \ Weight is sum_value
* \param
* \return
*
*/
/**< 2th example */
/** \brief
* There is a backpack that can hold items weighing S, existing n Item ,
* \ Ask if it's possible to n Select several items from the items and put them into the backpack , Make it put in
* \ The sum of the weights is just S
* \return
*
*/
/** \brief Depth first search analysis
* Knap(index,sum_value) Indicates that at this time, it is right for index Select items , Selected items
* \ Weight is sum_value
* \param
* \return
*
*/
#include <iostream>
#include <vector>
using namespace std;
const int maxn=10;
int matr[maxn];
int n;
double s;
vector<int> ans,temp;
void Knap(int index,double sum_value);
void output();
int pos=1;
int main()
{
cout << " Enter the number of items and backpack capacity :\n";
cin >> n >> s;
cout << " Input " << n<< " The weight of an item :\n";
for(int i=1;i<=n;++i)
cin >> matr[i];
Knap(1,0);
return 0;
}
void Knap(int index,double sum_value)
{
if(index>n||sum_value>s)
return;
if(sum_value==s)
{
ans=temp;
output();
return ;
}
temp.push_back(index);
if(sum_value+matr[index]<=s)
Knap(index+1,sum_value+matr[index]);
temp.pop_back();
Knap(index+1,sum_value);
}
void output()
{
cout << pos++ << "th:\n";
for(size_t i=0;i<ans.size();++i)
{
cout << "choose " << ans[i] << "th\n";
}
}
边栏推荐
- Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
- A simple and beautiful regression table is produced in one line of code~
- IMS data channel concept of 5g vonr+
- pmp真的有用吗?
- PMP证书有没有必要续期?
- 全链路压测:影子库与影子表之争
- 批量归一化(标准化)处理
- npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
- 5G VoNR+之IMS Data Channel概念
- 《二》标签
猜你喜欢
随机推荐
If you‘re running pod install manually, make sure flutter pub get is executed first.
想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
ServiceMesh主要解决的三大痛点
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
LabVIEW在打开一个新的引用,提示内存已满
Thread和Runnable创建线程的方式对比
01机器学习相关规定
JS variable case output user name
3.基金的类型
Timer创建定时器
STM32 system timer flashing LED
拿到PMP认证带来什么改变?
带你遨游银河系的 10 种分布式数据库
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
offer如何选择该考虑哪些因素
Decorator basic learning 02
Factor analysis r practice (with R installation tutorial and code)
ASP. Net MVC - resource cannot be found error - asp Net MVC – Resource Cannot be found error
The execution order of return in JS' try catch finally