当前位置:网站首页>与利润无关的背包问题(深度优先搜索)
与利润无关的背包问题(深度优先搜索)
2022-07-06 23:08:00 【疯疯癫癫才自由】
/**< 1th example) */
/** \brief
* 在九件物品中选出3件使其重量和500克之差的绝对值最小
* \求出物体编号
* \param
* \return
*
*/
/**< 循环求解,循环求解只能处理选择特定的物品数量,不具有普遍性; */
/**< 1th example) */
/** \brief
* 在九件物品中选出3件使其重量和500克之差的绝对值最小
* \求出物体编号
* \param
* \return
*
*/
/**< 循环求解 */
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n=9,abs_cha=1000;
int matr[n+1];
cout << "输入" << n << "件物品的重量:\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
* 在n件物品中选出k件使其重量和500克之差的绝对值最小
* \求出物体编号
* \param
* \return
*
*/
/**< 深度优先搜索求解 */
/** \brief
* 在n件物品中选出k件使其重量和500克之差的绝对值最小
* \求出物体编号
* \param
* \return
*
*/
/**< 深度优先搜索求解 */
/**
#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 << "输入物品的总数量以及要选择的物品数量:\n";
cin >> n >> sum;
cout << "输入" << n << "件物品的重量:\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)//< 这里不能加绝对值
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) //这样能稍微节约一点时间,不至于每个物品都去选择
DFS(index+1,sum+1,weight+matr[index]); //因为abs_cha一定是一个非负数,当weight+matr[index]-500
temp.pop_back(); //比abs_cha还大时,那么这个组合一定不是最佳选择的组合
DFS(index+1,sum,weight);
}
*/
/**< 2th example */
/** \brief
* 有一个背包可以放入的物品重量为S,现有n件物品,
* \ 问能否从这n件物品中选择若干件放入背包,使得放入的
* \ 重量之和恰为S
* \return
*
*/
/** \brief 递归设计分析
* Knap(s,n)表示背包的容量还剩余s,还有n件物品没进行选择
* \
* \param
* \return
*
*/
这个递归函数蛮不错的,直接在递归函数里输出下标,可以学习学习。这是在算法设计与分析教材上看的。
/**< 2th example */
/** \brief
* 有一个背包可以放入的物品重量为S,现有n件物品,
* \ 问能否从这n件物品中选择若干件放入背包,使得放入的
* \ 重量之和恰为S
* \return
*
*/
/** \brief 递归设计分析
* Knap(s,n)表示背包的容量还剩余s,还有n件物品没进行选择
* \
* \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 << "输入物品数量及背包容量:\n";
cin >> n >> s;
cout << "输入" << n<< "件物品的重量:\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) //选择第unselected件物品
{
cout << "choose : " << unselected << "th" << endl;
return true;
}
else
return Knap(rest,unselected-1);//不选择第unselected件物品
}
/**< 2th example */
/** \brief
* 有一个背包可以放入的物品重量为S,现有n件物品,
* \ 问能否从这n件物品中选择若干件放入背包,使得放入的
* \ 重量之和恰为S
* \return
*
*/
/** \brief 深度优先搜索分析
* Knap(index,sum_value)表示此时对第index件物品进行选择,已选择的物品
* \ 重量为sum_value
* \param
* \return
*
*/
/**< 2th example */
/** \brief
* 有一个背包可以放入的物品重量为S,现有n件物品,
* \ 问能否从这n件物品中选择若干件放入背包,使得放入的
* \ 重量之和恰为S
* \return
*
*/
/** \brief 深度优先搜索分析
* Knap(index,sum_value)表示此时对第index件物品进行选择,已选择的物品
* \ 重量为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 << "输入物品数量及背包容量:\n";
cin >> n >> s;
cout << "输入" << n<< "件物品的重量:\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";
}
}
边栏推荐
- If you ask me about R code debugging, I will tell you head, STR, help
- ThinkPHP关联预载入with
- Basic knowledge of road loss of 3GPP channel model
- 【QT】自定义控件-Loading
- STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
- Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
- DBSync新增对MongoDB、ES的支持
- 《五》表格
- Function pointer and pointer function in C language
- 批量归一化(标准化)处理
猜你喜欢
JDBC link Oracle reference code
Common Oracle SQL statements
Error: No named parameter with the name ‘foregroundColor‘
深入解析Kubebuilder
Time complexity & space complexity
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
MySQL数据库(基础篇)
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
[email protected] Mapping relatio"/>
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
A simple and beautiful regression table is produced in one line of code~
随机推荐
CentOS 7.9安装Oracle 21c历险记
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
[Yugong series] go teaching course 005 variables in July 2022
Two methods of chromosome coordinate sequencing
C语言中函数指针与指针函数
一个酷酷的“幽灵”控制台工具
Leetcode(46)——全排列
Using thread class and runnable interface to realize the difference between multithreading
5G VoNR+之IMS Data Channel概念
最全常用高数公式
Analyse approfondie de kubebuilder
Ansible概述和模块解释(你刚走过了今天,而扑面而来的却是昨天)
NiO related knowledge points (I)
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
JS variable
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
ClickHouse(03)ClickHouse怎么安装和部署
Sublime tips
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
When knative meets webassembly