调用c++递归库文件解决背包问题

简单背包问题


一、需求分析

前几天数据结构做了一个最简单的背包问题,具体需求如下
需求分析


二、个人思路

个人的想法是开辟两个数组:

数组①存储各个物品的体积
数组②以0 1 的形式存储物品对应是否存在(1表示存在,0表示不存在)

求和的方法

(1)数组②按照递归的深度依次把数组中的1的个数从0开始递增,每深入一层递归增加一个数组中1的数目,直到数组最大数目N;

(2)调用c++库文件next_permutation全排列数组②中的0与1的次序

(3)求和(设数组①为A[X]数组②为B[X])

   sum = A[0]B[0] + ....... + A[X-1]B[X-1] ;

(4)每一次计算之后都判断sum是否目标体积大小相等


三、实验截图

实验数据


四、具体代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <sstream>
using namespace std ;
int main()
{
    stringstream stream ;
    string total_number ;//插入临时处理变量
 //------------------------------------------------------------------------------------
 //插入所要用的货物的数目
    while(1)
    {   
        int i = 0 ;
        cout << "Enter the total goods' total number:" ;
        getline( cin , total_number ) ;
        for(i = 0 ; i < total_number.size() ; i ++ )
        {
            if( total_number[i] < '0' || total_number[i] > '9'  )
            {
                cout << "You enter the wrong number " << endl ;
                break ;
            }
        }
        if( i == total_number.size() )
        {
            break ;
        }
    }
    stream << total_number ;
    double number = 0 ;
    stream >> number ;
    stream.clear() ;
//-----------------------------------------------------------------------------
//  设置所要求的货物的体积和
    while(1)
    {   
        int i = 0 ;
        cout << "Enter the total weights you want to add :" ;
        getline( cin , total_number ) ;
        for(i = 0 ; i < total_number.size() ; i ++ )
        {
            if( total_number[i] < '0' || total_number[i] > '9'  )
            {
                cout << "You enter the wrong number " << endl ;
                break ;
            }
        }
        if( i == total_number.size() )
        {
            break ;
        }
    }
    double the_total_number = 0 ;
    stream << total_number ;
    stream >> the_total_number ;
    stream.clear() ;

//------------------------------------------------------------------
//插入每个货物的体积
    vector<double> my_goods_number ; 
    for( int i = 0 ; i < number ; i ++ )
    {
        string temp_number ;
        while(1)
        {
            int i = 0 ;
            cout << "Enter every goods' weight:" ;
            getline( cin , temp_number ) ;
            for(i = 0 ; i < temp_number.size() ; i ++ )
            {
                if( temp_number[i] < '0' || temp_number[i] > '9'  )
                {
                    cout << "You enter the wrong number " << endl ;
                    break ;
                }
            }
            if( i == temp_number.size() )
            {
                break ;
            }
        }
        double temp = 0 ;
        stream << temp_number ;        
        stream >> temp ;
        my_goods_number.push_back(temp) ;
        stream.clear() ;
    }

    vector<int> temp_choice(number) ;//插入临时变量
    size_t total = number ;
 //-----------------------------------------------------------------

 //开始求和   
    for(size_t oneCount = 0 ; oneCount < total ; oneCount ++ )
    {
        for(size_t i = 0 ; i < total ; i ++ )
        {
            //cout << i << endl ;
            temp_choice[i] = not( i < oneCount ) ;
        }
        do
        {
            int sum = 0 ; 
            for(size_t i = 0 ; i < total ; i ++ )
            {
                sum += my_goods_number[i] * temp_choice[i] ;
                //cout << temp_choice[i] << endl ;
            }
            if( sum == the_total_number )
            {   
                int judge = 0 ;
                cout << '(' ;
                for(int j = 0 ; j < total ; j ++ )
                {
                    if(temp_choice[j])
                        cout << my_goods_number[j] ;
                    else
                        continue ;
                    if( judge < total - oneCount - 1 )
                    {
                      cout << ',' ;
                      judge ++ ;
                    }

                }
                cout << ')' << endl;
            }
        }while(std::next_permutation(temp_choice.begin(),temp_choice.end())) ;
    } //调用c++递归库文件实现对数组B的全排列
    return 0 ;
}

感谢各位的阅读,有什么想法可以在下方评论区提出,欢迎各位批评指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
穷举法(也称为暴力搜索)是一种简单直接的解决问题的方法,它通过尝试所有可能的解决方案来找到最优解。在背包问题中,穷举法可以用来找到能够装入背包的物品组合,使得总价值最大化。 以下是使用穷举法解决背包问题的一般步骤: 1. 定义背包的容量和物品的重量、价值数组。 2. 枚举所有可能的物品组合,对于每个组合计算总重量和总价值。 3. 如果总重量小于等于背包容量,并且总价值大于当前最优解,则更新最优解。 4. 继续枚举下一个物品组合,直到所有组合都被尝试过。 5. 返回最优解。 在C++中,可以使用递归函数来实现穷举法解决背包问题。下面是一个简单的示例代码: ```cpp #include <iostream> using namespace std; int max_value = 0; // 最优解的总价值 // 递归函数,用于穷举所有可能的物品组合 void exhaustiveSearch(int capacity, int weights[], int values[], int n, int cur_weight, int cur_value) { if (cur_weight <= capacity && cur_value > max_value) { max_value = cur_value; } if (n == 0) { return; } // 不选择当前物品 exhaustiveSearch(capacity, weights, values, n - 1, cur_weight, cur_value); // 选择当前物品 exhaustiveSearch(capacity, weights, values, n - 1, cur_weight + weights[n - 1], cur_value + values[n - 1]); } int main() { int capacity = 10; // 背包容量 int weights[] = {2, 3, 4, 5}; // 物品重量数组 int values[] = {3, 4, 5, 6}; // 物品价值数组 int n = sizeof(weights) / sizeof(weights[0]); // 物品数量 exhaustiveSearch(capacity, weights, values, n, 0, 0); cout << "最优解的总价值为:" << max_value << endl; return 0; } ``` 这段代码中,我们定义了一个全局变量`max_value`来保存最优解的总价值。`exhaustiveSearch`函数用于递归地穷举所有可能的物品组合,并更新最优解。在`main`函数中,我们定义了背包的容量、物品的重量和价值数组,并调用`exhaustiveSearch`函数来解最优解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值