资源简介
c++数据结构实现经典背包问题,课程作业,供大家参考~~

代码片段和文件信息
# include
using namespace std;
# include
void bag_solve( int int int * );
void print_bag( int int int * );
int main()
{
int bag_Volume;//volume
cout << “Please input the total volume of the bag : “;
cin >> bag_Volume;
int num_of_objects; //total number of objects
cout << “Please input the total number of objects : “;
cin >> num_of_objects;
cout << “Please input the volume of each object : “;
int *arr = new int[num_of_objects];
for ( int i = 0; i < num_of_objects; i++ )
{
cin >> arr[i];
}
cout << “All the adapted combinations : “ << endl;
bag_solve( bag_Volume num_of_objects arr);
return 0;
}
//to find out all of the adapted combinations
void bag_solve( int bag_Volume int num_of_objects int *arr )
{
for ( int i = 1; i <= pow( 2 num_of_objects ); i++ )
{
int temp = i;
int sum_of_volume = 0;
for( int j = 0; j < num_of_objects; j++ )
{
int temp1 = temp % 2;
sum_of_volume += arr[j] * temp1;
temp = temp / 2;
if ( temp == 0 )
break;
}
if ( sum_of_volume == bag_Volume )//print out the adapted combination
print_bag( i num_of_objects arr );
}
}
//print out the adapted combination
void print_bag( int data int num_of_objects int *arr )
{
int temp = data;
for( int j = 0; j < num_of_objects; j++ )
{
if ( temp % 2 == 1 )
cout << arr[j] << “ “;
temp = temp / 2;
}
cout << endl;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-01-19 15:33 背包问题\
目录 0 2013-01-19 15:33 背包问题\Debug\
文件 573520 2010-11-17 01:56 背包问题\Debug\bag.exe
文件 805532 2010-11-17 01:56 背包问题\Debug\bag.ilk
文件 249887 2010-11-17 01:56 背包问题\Debug\bag.obj
文件 2034312 2010-11-16 15:48 背包问题\Debug\bag.pch
文件 1106944 2010-11-17 01:56 背包问题\Debug\bag.pdb
文件 74752 2010-11-17 01:56 背包问题\Debug\vc60.idb
文件 110592 2010-11-17 01:56 背包问题\Debug\vc60.pdb
文件 1480 2010-11-17 01:56 背包问题\bag.cpp
文件 3365 2010-11-17 00:52 背包问题\bag.dsp
文件 531 2010-11-17 01:56 背包问题\bag.dsw
文件 573520 2010-11-17 01:56 背包问题\bag.exe
文件 41984 2010-11-17 01:56 背包问题\bag.ncb
文件 48640 2010-11-17 01:56 背包问题\bag.opt
文件 735 2010-11-17 01:56 背包问题\bag.plg
- 上一篇:C++矩阵处理工具——Eigen3
- 下一篇:冒泡排序MFC实现
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
评论
共有 条评论