資源簡介
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?jects;?i++?)
{
cin?>>?arr[i];
}
cout?<“All?the?adapted?combinations?:?“?< 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?jects;?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?jects;?j++?)
{
if?(?temp?%?2?==?1?)
cout?<
temp?=?temp?/?2;
}
cout?<}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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實現
評論
共有 條評論