jm823454460 2023-11-28 11:46 采纳率: 0%
浏览 10

回溯法求解01背包问题,Clion运行错误

用回溯法求解01背包问题

#include <iostream>
#include<vector>

using namespace std;
#define N 4
int n = N ;                 //物品个数
vector<int> w;          //重量
vector<int> v;          //价值
int c = 13;                 //背包容量
int value = 0;              //当前价值
int weight = 0;             //当前重量
vector<int> path;           //  当前解空间
vector< vector<int> > result ;    //存放所有解
int OptimaValue = 0;                //最优价值
vector<int> OptimaSolution(N);//        最优解


void backtracking(int Anext){
    if(path.size() == N){
        if(value > OptimaValue){        //如果当前价值大于最优价值
            OptimaSolution=path;
            OptimaValue = value;
        }
        result.push_back(path);
        path.clear();
        return;
    }

    for(int i = 0;i <= 1; i++){                                       //两个分支,当前第Anext个物品是否放入
        if(i == 1 && w[Anext] + weight <= c){                            //重量小于背包容量,选择放入
                path.push_back(1);
                value += v[Anext];
                weight += w[Anext];
                backtracking( Anext+1);
                path.pop_back();
                path.push_back(0);
                value -= v[Anext];
                weight -= w[Anext];
        }
        else{                               //不放入
            path.push_back(0);
            backtracking(Anext+1);
            path.pop_back();
        }
    }

}
void out(){
    backtracking(0);        //从第0个物品开始
    for(const auto &a: result){  //遍历所有解
        for(const auto &b:a){
            cout<< b <<" ";
        }
        cout<< "\"" <<endl;
    }
    return;
}
int main() {
    std::cout << "Hello, World!" << std::endl;
    v.push_back(12);
    v.push_back(11);
    v.push_back(9);
    v.push_back(8);

    w.push_back(8);
    w.push_back(6);
    w.push_back(4);
    w.push_back(3);
    
    out();
    return 0;
}

img


用clion运行的时候,Anext=0,递归调用backtracking(Anext+1)时候,Anext直接变为1000多
报 信号=SIGSEGV(Segmentation fault)
望解答

  • 写回答

4条回答 默认 最新

  • 赵4老师 2023-11-28 14:05
    关注

    仅供参考:

    #include <time.h>
    #include <stdlib.h>
    #include <windows.h>
    int main() {
        int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
    
        srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
        while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
            b[(a=rand()%11)]=0;
            Sleep(100);
            b[(a=rand()%11)]=1;
            Sleep(100);
            b[(a=rand()%11)]=2;
            Sleep(100);
            b[(a=rand()%11)]=3;
            Sleep(100);
            b[(a=rand()%11)]=4;
            Sleep(100);
        }
        return 0;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月28日

悬赏问题

  • ¥15 暴雪战网api相关问题
  • ¥15 而使用UE5引擎的 工具选项里 打开c++ visual studio 就会有部分显示加载失败 如图 加载失败的这张图 请问是什么原因
  • ¥15 mysql 对多个字段模糊查询,返回第一个匹配的字段
  • ¥15 the testing results of the whole dataset is empty
  • ¥15 can问题,往哥解决
  • ¥15 FFmpeg 成功推流到 Nginx RTMP 服务器但无法用 ffplay 或 VLC 播放
  • ¥15 请修改以下C语言代码使其能正确输出最短路径
  • ¥20 抖音商城拉码器安卓报错求解决办法或者有新的拉码脚本也可以介绍一下
  • ¥15 MPLAB IDE V2.35 报错make[2]: *** [build/default/production/_ext/1472/MSSP_I2C.p1] Error 1
  • ¥15 在国外文献网站里点击view pdf 加载异常缓慢甚至加载不出来。