c++数据结构实现经典背包问题
c++数据结构实现经典背包问题,课程作业,供大家参考~~
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-01-19 15:33 背包问题
目录 0 2013-01-19 15:33 背包问题Debug
文件 573520 2010-11-17 01:56 背包问题Debugag.exe
文件 805532 2010-11-17 01:56 背包问题Debugag.ilk
文件 249887 2010-11-17 01:56 背包问题Debugag.obj
文件 2034312 2010-11-16 15:48 背包问题Debugag.pch
文件 1106944 2010-11-17 01:56 背包问题Debugag.pdb
文件 74752 2010-11-17 01:56 背包问题Debugvc60.idb
文件 110592 2010-11-17 01:56 背包问题Debugvc60.pdb
文件 1480 2010-11-17 01:56 背包问题ag.cpp
文件 3365 2010-11-17 00:52 背包问题ag.dsp
文件 531 2010-11-17 01:56 背包问题ag.dsw
文件 573520 2010-11-17 01:56 背包问题ag.exe
文件 41984 2010-11-17 01:56 背包问题ag.ncb
文件 48640 2010-11-17 01:56 背包问题ag.opt
文件 735 2010-11-17 01:56 背包问题ag.plg
# 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 背包问题Debugag.exe
文件 805532 2010-11-17 01:56 背包问题Debugag.ilk
文件 249887 2010-11-17 01:56 背包问题Debugag.obj
文件 2034312 2010-11-16 15:48 背包问题Debugag.pch
文件 1106944 2010-11-17 01:56 背包问题Debugag.pdb
文件 74752 2010-11-17 01:56 背包问题Debugvc60.idb
文件 110592 2010-11-17 01:56 背包问题Debugvc60.pdb
文件 1480 2010-11-17 01:56 背包问题ag.cpp
文件 3365 2010-11-17 00:52 背包问题ag.dsp
文件 531 2010-11-17 01:56 背包问题ag.dsw
文件 573520 2010-11-17 01:56 背包问题ag.exe
文件 41984 2010-11-17 01:56 背包问题ag.ncb
文件 48640 2010-11-17 01:56 背包问题ag.opt
文件 735 2010-11-17 01:56 背包问题ag.plg
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)