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

本文介绍了一种解决简单背包问题的方法,通过使用两个数组分别存储物品体积和存在状态,并利用C++标准库函数next_permutation进行全排列组合,最终找出符合特定体积要求的所有可能组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单背包问题


一、需求分析

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


二、个人思路

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

数组①存储各个物品的体积
数组②以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 ;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值