用数组模拟栈实现递归函数模拟

news2024/11/23 15:00:30

做算法课设时候看到题目要求模拟函数递归时候栈的入栈出栈过程。本来想着直接调用系统递归函数即可,可是发现系统函数栈的空间非常小大约只有3000层,很容易爆栈。于是便有了用栈去模拟递归函数的想法,但是上网查了下貌似相关代码比较少,让GPT写的代码又牛头不对马嘴,于是手搓了一下。用栈模拟递归的过程,对递归过程,栈的理解都很有帮助。

在这里插入图片描述

二分递归函数模拟

第一个函数erfen为正常的二分递归函数,但是系统的递归栈最多能递归到3000层左右,太少了。我们可以用栈来模拟函数递归的过程,这样栈的层数可以达到我们设置的数组大小,我这里设置了二十万。

#include <iostream>

using namespace std;

const int N = 2e5 + 5;

struct binaryFrame {
    int l, r;
    int level;//记录当前层数
    int step;//记录步数
}stackBinary[N];

/*需要模拟的二分递归函数
int erfen(int arr[], int l, int r, int key)
{
    int ans = -1;


    int mid = l + r >> 1;

    if (arr[mid] == key && l == r) return r;

    if (l >= r) return -1;
    if (arr[mid] >= key) {
        ans = erfen(arr, l, mid, key);
    }
    else {
        ans = erfen(arr, mid + 1, r, key);
    }

    return ans;
}
*/

void binarySearch(int arr[], int l, int r, int key, int& index)
{
    int nowLevel = 1, nowStep = 0, tt = 0, L = l, R = r;
    tt++;
    stackBinary[tt] = { l,r,nowLevel,nowStep };
    cout << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
    while (L < R)
    {
        int mid = L + R >> 1;
        if (arr[mid] >= key)
        {
            R = mid;
            stackBinary[++tt] = { L,R,++nowLevel,++nowStep };
            cout << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
        }
        else {
            L = mid + 1;
            stackBinary[++tt] = { L,R,++nowLevel,++nowStep };
            cout << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
        }
    }
    if (arr[R] == key) {
        index = R;
    }

    while (tt > 0)
    {
        cout << "出栈:" << "当前执行函数纸带范围为:(" << stackBinary[tt].l << " , " << stackBinary[tt].r << " )" << " 层数为" << stackBinary[tt].level << " 执行步数为" << stackBinary[tt].step << "\n";
        tt--;
    }

}


int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
    int n = 10;
    int key = 2;
    int index = -1;
    binarySearch(arr, 0, n - 1, key, index);
    if (index == -1) {
        cout << "找不到这个吊数\n";
    }
    else {
        cout << "这个B数在数组中的第" << index + 1 << "个位置" << "\n";
    }

    return 0;
}

显示压栈出栈过程
在这里插入图片描述

栈模拟回溯法解决01背包

#include<iostream>
#include<vector>

using namespace std;

typedef long long LL;

const int N = 2e5 + 5;

/*
// 全局变量用于存储最终结果
int max_value = 0;
vector<int> best_set;

/*
// 需要模拟的递归回溯函数
void knapsack(int index, int current_weight, int current_value, int capacity, const vector<int>& weights, const vector<int>& values, vector<int>& chosen_items) {
	// 基础情况:如果考虑完所有物品
	if (index == weights.size()) {
		// 检查当前物品集合是否是一个有效解
		if (current_weight <= capacity && current_value > max_value) {
			max_value = current_value;
			best_set = chosen_items;
		}
		return;
	}

	// 选择当前物品
	chosen_items.push_back(index);
	knapsack(index + 1, current_weight + weights[index], current_value + values[index], capacity, weights, values, chosen_items);
	chosen_items.pop_back(); // 回溯

	// 不选择当前物品
	knapsack(index + 1, current_weight, current_value, capacity, weights, values, chosen_items);
}*/


struct backFrame
{
	int index;
	int current_weight;
	int current_value;
	int step;
	vector<int> chosenItems;
	int stage;
}stackBack[N];

void backBag(int n, int capacity, int& max_value, vector<int>& weights, vector<int>& values, vector<int>& chosenItems, vector<int>& best_set)
{
	int tt = 0, nowStep = 0;
	stackBack[++tt] = { 0,0,0,0,{},0 };
	cout << "压栈:初始化压栈,压入 0 0 0 0 {} 0" << "\n";

	while (tt > 0)
	{
		backFrame now = stackBack[tt];
		cout << "当前执行函数层为:" << now.index << " " << now.current_weight << " " << now.current_value << " " << now.step << "\n";
		if (stackBack[tt].index >= n) {
			if (stackBack[tt].current_value > max_value && stackBack[tt].current_weight <= capacity)
			{
				max_value = stackBack[tt].current_value;
				best_set = stackBack[tt].chosenItems;
				cout << "更新best_set ";
				for (auto i : best_set) cout << i + 1 << " ";
				cout << "\n";
			}
			
			tt--;
			cout << "出栈1:" << now.index  << " " << now.current_weight<< " " << now.current_value << " " << now.step << "\n";
			continue;
		}

		

		if (now.stage == 0) {//栈顶为初始状态的话直接下一层函数压栈
			chosenItems.push_back(now.index);
			stackBack[++tt] = { now.index + 1,now.current_weight + weights[now.index],now.current_value + values[now.index],now.step + 1,chosenItems,0 };
			cout << "压栈1: " << now.index + 1 << " " << now.current_weight + weights[now.index] << " " << now.current_value + values[now.index] << " " << now.step + 1 << "\n";
			
			stackBack[tt - 1].stage ++;
		}
		else if (now.stage == 1) {
            chosenItems.pop_back();
			stackBack[++tt] = { now.index + 1,now.current_weight,now.current_value,now.step + 1,chosenItems,0 };
			cout << "压栈2: " << now.index + 1 << " " << now.current_weight << " " << now.current_value  << " " << now.step + 1 << "\n";
			stackBack[tt - 1].stage ++;
		}
		else if (now.stage == 2) {
			tt--;
			cout << "出栈2:" << now.index  << " " << now.current_weight<< " " << now.current_value << " " << now.step << "\n";
		}

	}

}

int main()
{
	vector<int> weights = { 1,2,3,5,7,6 };
	vector<int> values = { 6,5,10,30,15,25 };
	vector<int> chosenItems;
	vector<int> best_set;
	int capacity = 10, n = 6;
	int max_value = -1;
	/*
	cout << "请输入物品件数和背包容量\n";
	cin >> n >> capacity;
	cout << "请输入" << n << "件物品,每件物品的质量,价值\n";
	for (int i = 0; i < n; i++)
	{
		int x, y;
		cin >> x >> y;
		weights.push_back(x);
		values.push_back(y);
	}*/

	backBag(n, capacity, max_value, weights, values, chosenItems, best_set);

	LL sum = 0;

	for (auto i : best_set)
	{
		cout << "选择了第" << i + 1 << "件物品\n";
		sum += weights[i];
	}
	cout << "可以获得的最大价值为" << max_value << "\n";
	cout << "所占用的背包空间为" << sum << "\n";

	return 0;
}

/*
6 10
1 6
2 5
3 10
5 30
7 15
6 25
*/

在这里插入图片描述

栈模拟备忘录法解决01背包

#include <iostream>
#include <vector>
#include <cstring>
#include<Windows.h>

using namespace std;

const int N = 2e4 + 5;


int n, W;

int dp[505][1005]; // 备忘录数组
vector<int> weights;
vector<int> values;

/*需要模拟的备忘录递归函数
// 递归函数,包含备忘录算法逻辑,改为void类型
void knapsack(int idx, int W, int N, int& maxVal) {
    // 基本情况
    if (idx == N || W == 0) {
        maxVal = 0;
        cout << "边界:" << idx << " " << W << " " << maxVal << "\n";
        return;
    }
    // 检查是否已经计算过
    if (dp[idx][W] != -1) {
        maxVal = dp[idx][W];
        cout << "已经计算过:" << idx << " " << W << " " << maxVal << "\n";
        return;
    }
    // 选择不包含当前物品
    knapsack(idx + 1, W, N, maxVal);
    int without = maxVal; // 从上一次递归中获取结果
    cout << "without:" << idx << " " << W << " " << without << "\n";

    int with = 0;
    // 选择包含当前物品,如果背包容量足够
    if (W >= weight[idx]) {
        knapsack(idx + 1, W - weight[idx], N, with);
        with = values[idx] + with; // 添加当前物品的价值
        cout << "with:" << idx << " " << W << " " << with << "\n";
    }

    // 计算最大价值并存储备忘录结果
    maxVal = max(without, with);
    dp[idx][W] = maxVal; // 存储备忘录结果
    //cout<<"idx=,W=,dp= "<<idx<<" "<<W<<" "<<dp[idx]
}*/


struct memoFrame
{
    int index;
    int with;
    int without;
    int max_value;
    int capacity;
    int stage;
}stackMemo[N];

void memoBag(int index, int capacity, vector<int>& weights, vector<int>& values)
{
    int tt = 0;
    stackMemo[++tt] = { 0,0,0,0,capacity,0 };

    int max_value = 0;

    while (tt > 0)
    {
        //cout << "cnt=" << cnt << "\n";
        memoFrame& now = stackMemo[tt];
        //cout << "当前函数层为,index,capacity,stage=" << now.index << " " << now.capacity << " " << now.stage << "\n";
        if ((now.index == n || now.capacity <= 0)) {
            memoFrame& last = stackMemo[tt - 1];
            last.max_value = 0;
            //cout << "当前层为:,返回给上一层的返回值为" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            tt--;
            //cout << "出栈:index,capacity,return" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            continue;
        }
        if (dp[now.index][now.capacity] != -1)
        {
            memoFrame& last = stackMemo[tt - 1];
            last.max_value = dp[now.index][now.capacity];
            //cout << "当前层为:,返回给上一层的返回值为" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            tt--;
            //cout << "出栈:index,capacity,return" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            continue;
        }

        if (now.stage == 0)
        {
            stackMemo[++tt] = { now.index + 1,0,0,0,now.capacity,0 };
            //cout << "压栈0:" << now.index + 1 << " " << now.capacity << " " << "\n";
            now.stage++;
        }
        else if (now.stage == 1)
        {
            now.without = now.max_value;
            //cout << "更新without:index, without:" << now.index << " " << now.without << "\n";
            now.stage++;
        }
        else if (now.stage == 2)
        {
            if (now.capacity >= weights[now.index]) stackMemo[++tt] = { now.index + 1, 0, 0, 0, now.capacity - weights[now.index], 0 };
            // cout << "压栈1:" << now.index + 1 << " " << now.capacity << " " << "\n";
            now.stage++;
        }
        else if (now.stage == 3)
        {
            if (now.capacity >= weights[now.index])
            {
                now.with = now.max_value + values[now.index];
                //cout << "更新with, index,with:" << now.index << " " << now.with << "\n";
            }
            now.stage++;
        }
        else if (now.stage == 4)
        {
            memoFrame& last = stackMemo[tt - 1];
            dp[now.index][now.capacity] = max(now.with, now.without);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    cout << dp[i][j] << " ";
                }
                cout << "\n";
            }

            last.max_value = dp[now.index][now.capacity];
            Sleep(1000);
            //cout << "更新dp: index,dp:" << now.index << " " << last.max_value << "\n";
            tt--;
            //cout << "出栈:index,capacity,return" << now.index << " " << now.capacity << " " << last.max_value << "\n";
        }
    }

}


int main() {
    cin >> n >> W;; // 读取物品数量和背包容量

    weights.resize(n);
    values.resize(n);

    for (int i = 0; i < n; ++i) {
        cin >> weights[i] >> values[i];
    }

    memset(dp, -1, sizeof(dp));


    memoBag(0, W, weights, values);


    cout << "最大价值:" << dp[0][W] << endl;

    return 0;
}

/*
6 10
1 6
2 5
3 10
5 30
7 15
6 25
*/

在这里插入图片描述

把三个题整合到一块

#include <iostream>
#include <vector>
#include <cstring>
#include<iomanip>
#include <windows.h>

using namespace std;

const int N = 2e4 + 5;

typedef long long LL;

int n, W;//n可以是二分的数组长度,也可以是背包的物品个数,W是背包容量

int ansPos=-1;//二分搜索到的答案位置

int arr[N];

int dp[505][1005]; // 备忘录数组
vector<int> weights;
vector<int> values;

struct binaryFrame {
    int l, r;
    int level;//记录当前层数
    int step;//记录步数
}stackBinary[N];

struct backFrame
{
    int index;
    int current_weight;
    int current_value;
    int step;
    vector<int> chosenItems;
    int stage;
}stackBack[N];

struct memoFrame
{
    int index;
    int with;
    int without;
    int max_value;
    int capacity;
    int stage;
}stackMemo[N];

void binarySearch(int l, int r, int key)
{
    int nowLevel = 1, nowStep = 0, tt = 0, L = l, R = r;
    tt++;
    stackBinary[tt] = { l,r,nowLevel,nowStep };
    cout<<right<<setw(50) << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
    Sleep(200);
    while (L < R)
    {
        int mid = L + R >> 1;
        if (arr[mid] >= key)
        {
            R = mid;
            stackBinary[++tt] = { L,R,++nowLevel,++nowStep };
            cout<<right<<setw(50) << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
            Sleep(200);
        }
        else {
            L = mid + 1;
            stackBinary[++tt] = { L,R,++nowLevel,++nowStep };
            cout<<right<<setw(50) << "压栈:" << "当前执行函数纸带范围为:(" << L << " , " << R << " )" << " 层数为" << nowLevel << " 执行步数为" << nowStep << "\n";
            Sleep(200);
        }
    }
    if (arr[R] == key) {
        ansPos = R;
    }

    while (tt > 0)
    {
        cout<<right<<setw(50) << "出栈:" << "当前执行函数纸带范围为:(" << stackBinary[tt].l << " , " << stackBinary[tt].r << " )" << " 层数为" << stackBinary[tt].level << " 执行步数为" << stackBinary[tt].step << "\n";
        Sleep(200);
        tt--;
    }

}

void backBag(int n, int capacity, int& max_value, vector<int>& weights, vector<int>& values, vector<int>& chosenItems, vector<int>& best_set)
{
    int tt = 0, nowStep = 0;
    stackBack[++tt] = { 0,0,0,0,{},0 };
    cout<<right<<setw(50) << "压栈:初始化压栈,压入 0 0 0 0 {} 0" << "\n";
    Sleep(200);

    while (tt > 0)
    {
        backFrame now = stackBack[tt];
        
        cout << right << setw(50) << "当前执行函数层为:下标为:" << now.index << " 当前重量:" << now.current_weight << " 当前价值:" << now.current_value << " 当前步数:" << now.step << "\n";
        Sleep(200);
        if (stackBack[tt].index >= n) {
            if (stackBack[tt].current_value > max_value && stackBack[tt].current_weight <= capacity)
            {
                max_value = stackBack[tt].current_value;
                best_set = stackBack[tt].chosenItems;
            }

            tt--;
            cout<<right<<setw(50) << "出栈1:出栈下标:" << now.index << " 当前重量:" << now.current_weight << " 当前价值:" << now.current_value << " 当前步数:" << now.step << "\n";
            Sleep(200);
            continue;
        }



        if (now.stage == 0) {//栈顶为初始状态的话直接下一层函数压栈
            chosenItems.push_back(now.index);
            stackBack[++tt] = { now.index + 1,now.current_weight + weights[now.index],now.current_value + values[now.index],now.step + 1,chosenItems,0 };
            cout<<right<<setw(50) << "压栈1: 压栈下标:" << now.index + 1 << " 压栈重量:" << now.current_weight + weights[now.index] << " 压栈价值:" << now.current_value + values[now.index] << " 压栈步数:" << now.step + 1 << "\n";
            Sleep(200);

            stackBack[tt - 1].stage++;
        }
        else if (now.stage == 1) {
            chosenItems.pop_back();
            stackBack[++tt] = { now.index + 1,now.current_weight,now.current_value,now.step + 1,chosenItems,0 };
            cout<<right<<setw(50) << "压栈2: 压栈下标:" << now.index + 1 << " 压栈重量:" << now.current_weight << " " << now.current_value << " " << now.step + 1 << "\n";
            Sleep(200);
            stackBack[tt - 1].stage++;
        }
        else if (now.stage == 2) {
            tt--;
            cout<<right<<setw(50) << "出栈2:" << now.index << " 出栈重量:" << now.current_weight << " 出栈价值:" << now.current_value << " 出栈步数" << now.step << "\n";
            Sleep(200);
        }

    }

}


void memoBag(int index, int capacity, vector<int>& weights, vector<int>& values)
{
    int tt = 0;
    stackMemo[++tt] = { 0,0,0,0,capacity,0 };

    int max_value = 0;

    while (tt > 0)
    {
        memoFrame& now = stackMemo[tt];
        cout<<right<<setw(50) << "当前函数层为,当前执行下标:" << now.index << " 当前背包剩余容量:" << now.capacity << " 当前stage:" << now.stage << "\n";
        Sleep(200);
        if ((now.index == n || now.capacity <= 0)) {
            memoFrame& last = stackMemo[tt - 1];
            last.max_value = 0;
            //cout << "当前层为:,返回给上一层的返回值为" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            tt--;
            cout<<right<<setw(50) << "出栈:出栈下标:" << now.index << " 出栈时剩余背包容量:" << now.capacity << " 出栈返回值:" << last.max_value << "\n";
            Sleep(200);
            continue;
        }
        if (dp[now.index][now.capacity] != -1)
        {
            memoFrame& last = stackMemo[tt - 1];
            last.max_value = dp[now.index][now.capacity];
            //cout << "当前层为:,返回给上一层的返回值为" << now.index << " " << now.capacity << " " << last.max_value << "\n";
            tt--;
            cout<<right<<setw(50) << "出栈:出栈下标:" << now.index << " 出栈时剩余背包容量:" << now.capacity << " 出栈时返回值:" << last.max_value << "\n";
            Sleep(200);
            continue;
        }

        if (now.stage == 0)
        {
            stackMemo[++tt] = { now.index + 1,0,0,0,now.capacity,0 };
            cout<<right<<setw(50) << "压栈0:压栈下标:" << now.index + 1 << " 压栈背包容量;" << now.capacity << " " << "\n";
            Sleep(200);
            now.stage++;
        }
        else if (now.stage == 1)
        {
            now.without = now.max_value;
            //cout << "更新without:index, without:" << now.index << " " << now.without << "\n";
            now.stage++;
        }
        else if (now.stage == 2)
        {
            if (now.capacity >= weights[now.index]) stackMemo[++tt] = { now.index + 1, 0, 0, 0, now.capacity - weights[now.index], 0 };
             cout<<right<<setw(50) << "压栈1:压栈下标:" << now.index + 1 << " 压栈背包容量:" << now.capacity-weights[now.index] << " " << "\n";
             Sleep(200);
            now.stage++;
        }
        else if (now.stage == 3)
        {
            if (now.capacity >= weights[now.index])
            {
                now.with = now.max_value + values[now.index];
                //cout << "更新with, index,with:" << now.index << " " << now.with << "\n";
            }
            now.stage++;
        }
        else if (now.stage == 4)
        {
            memoFrame& last = stackMemo[tt - 1];
            dp[now.index][now.capacity] = max(now.with, now.without);

            last.max_value = dp[now.index][now.capacity];
            //cout << "更新dp: index,dp:" << now.index << " " << last.max_value << "\n";
            tt--;
            cout<<right<<setw(50) << "出栈:出栈下标:" << now.index << " 出栈时背包容量:" << now.capacity << " 这一层出栈的返回值:" << last.max_value << "\n";
            Sleep(200);
        }
    }

}


int main() 
{
    int op;
    cout<<right<<setw(50) << "请选择要执行的递归函数模拟:1.二分搜索,2.回溯法,3.备忘录法\n";
    while(cin >> op) {
        if (op == 1) {
            int key;
            cout << right << setw(50) << "栈模拟递归二分算法\n\n\n";
            cout << right << setw(50) << "请输入查询数组长度\n";
            cin >> n;
            cout << right << setw(50) << "请输入" << n << "个数字\n";
            for (int i = 0; i < n; i++)
            {
                cin >> arr[i];
            }
            cout << right << setw(50) << "请输入需要查找的数字key\n";
            cin >> key;
            binarySearch(0, n - 1, key);
            if (ansPos == -1) cout << right << setw(50) << "找不到这个吊数\n";
            else cout << right << setw(50) << "数字" << key << "在数组中的第" << ansPos + 1 << "个位置\n";
        }
        else if (op == 2)
        {
            weights.clear();
            values.clear();

            cout << right << setw(50) << "栈模拟递归回溯算法\n\n\n";
            cout << right << setw(50) << "请输入物品件数和背包容量\n";
            cin >> n >> W;
            cout << right << setw(50) << "请输入" << n << "件物品的质量和价值\n";
            for (int i = 0; i < n; i++)
            {
                int x, y;
                cin >> x >> y;
                weights.push_back(x);
                values.push_back(y);
            }
            int max_value = -1;
            vector<int> best_set;
            vector<int> chosenItems;
            backBag(n, W, max_value, weights, values, chosenItems, best_set);
            cout << right << setw(50) << "可以获得的最大价值为:" << max_value << "\n";
            for (auto i : best_set)
            {
                cout << right << setw(50) << "选择了第" << i + 1 << "件物品\n";
            }
        }
        else if (op == 3)
        {
            weights.clear();
            values.clear();

            cout << right << setw(50) << "栈模拟递归备忘录算法\n\n\n";
            cout << right << setw(50) << "请输入物品件数和背包容量\n";
            cin >> n >> W;
            cout << right << setw(50) << "请输入" << n << "件物品的质量和价值\n";
            for (int i = 0; i < n; i++)
            {
                int x, y;
                cin >> x >> y;
                weights.push_back(x);
                values.push_back(y);
            }
            memset(dp, -1, sizeof(dp));


            memoBag(0, W, weights, values);
            cout << right << setw(50) << "能获得的最大价值为:" << dp[0][W] << "\n";
        }

        cout << right << setw(50) << "1.继续,2.结束\n";
        cin >> op;
        if (op == 2) break;
        cout << right << setw(50) << "请选择要执行的递归函数模拟:1.二分搜索,2.回溯法,3.备忘录法\n";

    }

    return 0;
}

/*二分搜索样例输入
10
1 2 3 4 5 6 7 8 9 10
2
*/

/*回溯与备忘录样例输入
6 10
1 6
2 5
3 10
5 30
7 15
6 25
*/

/*需要模拟的二分递归函数
int erfen(int arr[], int l, int r, int key)
{
    int ans = -1;


    int mid = l + r >> 1;

    if (arr[mid] == key && l == r) return r;

    if (l >= r) return -1;
    if (arr[mid] >= key) {
        ans = erfen(arr, l, mid, key);
    }
    else {
        ans = erfen(arr, mid + 1, r, key);
    }

    return ans;
}
*/

/*
// 需要模拟的递归回溯函数
void knapsack(int index, int current_weight, int current_value, int capacity, const vector<int>& weights, const vector<int>& values, vector<int>& chosen_items) {
    // 基础情况:如果考虑完所有物品
    if (index == weights.size()) {
        // 检查当前物品集合是否是一个有效解
        if (current_weight <= capacity && current_value > max_value) {
            max_value = current_value;
            best_set = chosen_items;
        }
        return;
    }

    // 选择当前物品
    chosen_items.push_back(index);
    knapsack(index + 1, current_weight + weights[index], current_value + values[index], capacity, weights, values, chosen_items);
    chosen_items.pop_back(); // 回溯

    // 不选择当前物品
    knapsack(index + 1, current_weight, current_value, capacity, weights, values, chosen_items);
}*/

/*需要模拟的备忘录递归函数
// 递归函数,包含备忘录算法逻辑,改为void类型
void knapsack(int idx, int W, int N, int& maxVal) {
    // 基本情况
    if (idx == N || W == 0) {
        maxVal = 0;
        cout << "边界:" << idx << " " << W << " " << maxVal << "\n";
        return;
    }
    // 检查是否已经计算过
    if (dp[idx][W] != -1) {
        maxVal = dp[idx][W];
        cout << "已经计算过:" << idx << " " << W << " " << maxVal << "\n";
        return;
    }
    // 选择不包含当前物品
    knapsack(idx + 1, W, N, maxVal);
    int without = maxVal; // 从上一次递归中获取结果
    cout << "without:" << idx << " " << W << " " << without << "\n";

    int with = 0;
    // 选择包含当前物品,如果背包容量足够
    if (W >= weight[idx]) {
        knapsack(idx + 1, W - weight[idx], N, with);
        with = values[idx] + with; // 添加当前物品的价值
        cout << "with:" << idx << " " << W << " " << with << "\n";
    }

    // 计算最大价值并存储备忘录结果
    maxVal = max(without, with);
    dp[idx][W] = maxVal; // 存储备忘录结果
    //cout<<"idx=,W=,dp= "<<idx<<" "<<W<<" "<<dp[idx]
}*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1872946.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【网络安全学习】漏洞利用:BurpSuite的使用-02-常用的标签

下面介绍一下BurpSuite各个标签的用法&#x1f447; 1️⃣ Dashboard标签 Dashboard&#xff0c;顾名思义就是BurpSuite的仪表盘&#xff0c;可以通过Dashboard进行漏洞扫描&#xff0c;不过该功能需要升级到pro版本&#xff0c;也就是得交钱&#x1f62d;。 2️⃣ Target标签…

STM32要学到什么程度才算合格?

在开始前刚好我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「嵌入式的资料从专业入门到高级教程」&#xff0c; 点个关注在评论区回复“888”之后私信回复“888”&#xff0c;全部无偿共享给大家&#xff01;&#xff01;&#xff01; STM32 这玩意儿要学到啥…

CDN入门

在腾讯云上使用CDN 1、参考 内容分发网络 CDN 从零开始配置 CDN-快速入门-文档中心-腾讯云 2、验证 访问&#xff1a; 登录 - 腾讯云 Window10本地电脑使用命令验证 nslookup -qt-cname hmblogs.com.cn Ubuntu下验证 dig hmblogs.com.cn

【等保小知识】政府机构哪些系统需要过等保?怎么过?

随着等保政策的严格落地执行&#xff0c;政府机构率先做榜样&#xff0c;积极办理等保。那你知道政府机构到底哪些系统需要过等保吗&#xff1f;这里我们小编就来给大家简单回答一下&#xff0c;仅供参考哈&#xff01; 政府机构哪些系统需要过等保&#xff1f; 【回答】&…

docker部署EKF

1.检查版本 检查当前系统的docker版本 [rootnode1 ~]# docker version Client: Docker Engine - CommunityVersion: 20.10.12API version: 1.41Go version: go1.16.12Git commit: e91ed57Built: Mon Dec 13 11:45:41 2021OS/Arch: …

基于SpringBoot养老院管理系统设计和实现(源码+LW+调试文档+讲解等)

&#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN作者、博客专家、全栈领域优质创作者&#xff0c;博客之星、平台优质作者、专注于Java、小程序技术领域和毕业项目实战✌&#x1f497; &#x1f31f;文末获取源码数据库&#x1f31f;感兴趣的可以先收藏起来&#xff0c;还…

SpringBoot集成道历(实现道历日期查询)

官网地址&#xff1a;官网地址https://6tail.cn/calendar/api.html 1、导入依赖 <dependency><groupId>cn.6tail</groupId><artifactId>lunar</artifactId><version>1.3.9</version></dependency><dependency><group…

未来先行!MWC 2024 世界移动通信大会盛大开幕!!!

2024MWC上海世界移动通信大会&#xff0c;在上海新国际博览中心&#xff08;SNIEC&#xff09;盛大开幕。 今年&#xff0c;MWC的主办方GSMA&#xff08;全球移动通信系统协会&#xff09;为这届MWC定下了一个主题——“Future First&#xff08;未来先行&#xff09;”。各大…

定制聚四氟乙烯架子离心管架子消解管样品架

南京瑞尼克定制离心管架子、消解管架子、微波罐架子等&#xff0c;各种实验室所需器皿样品架。 我司聚四氟乙烯架子优势&#xff1a; 1.耐强腐蚀&#xff08;耐各种强酸强碱和有机溶剂&#xff09; 2.耐高低温&#xff1a;-200至250℃ 3.工艺精制&#xff0c;尺寸&#xff0…

【代码随想录】【算法训练营】【第52天】 [647]回文子串 [516]最长回文子序列

前言 思路及算法思维&#xff0c;指路 代码随想录。 题目来自 LeetCode。 day 52&#xff0c;周五&#xff0c;开始补作业了~ 题目详情 [647] 回文子串 题目描述 647 回文子串 解题思路 前提&#xff1a; 思路&#xff1a; 重点&#xff1a; 代码实现 C语言 [516] 最…

Davtest一键远程编辑远程Web服务器上的文件(KALI工具系列三十三)

目录 1、KALI LINUX 简介 2、Davtest 工具简介 3、信息收集 3.1 目标IP&#xff08;服务器) 3.2 kali的IP 4、操作实例 4.1 常见漏洞扫描 4.2 身份验证 4.3 特定扫描 4.4 输出结果 5、总结 1、KALI LINUX 简介 Kali Linux 是一个功能强大、多才多艺的 Linux 发行版 &…

【笔记】echarts图表的缩放和鼠标滚动冲突的处理解决方案

解决方案不是很好&#xff0c;来源于github的issue&#xff0c;官方提供了&#xff0c;组合键触发缩放的功能。 https://github.com/apache/echarts/issues/5769 https://echarts.apache.org/zh/option.html#dataZoom-inside.zoomOnMouseWheel dataZoom-inside.zoomOnMouseWhe…

一文让你简单明了的知道云管理平台的作用

随着云计算的飞速发展&#xff0c;越来越多的企业实现了上云。因此云管理平台也在云计算环境中扮演着至关重要的角色&#xff0c;在企业上云后充分发挥作用。今天我们小编就来为大家简单讲解一下云管平台的作用。 一文让你简单明了的知道云管理平台的作用 作用1、提高工作效率…

Window.Notification API详细使用指南

在访问CSDN网站时,就会弹出窗口提示是否允许接收通知,刚开始以为是什么高深的技术,后来了解到是HTML5的一个通知特性. 基本定义 window.Notification 是Web API的一部分&#xff0c;它允许网页或应用程序向用户发送桌面通知。这些通知可以包含图像、文本和声音&#xff0c;以…

Idea 插件 Convert YAML and Properties File

YAML 和 Properties 相互转换插件 ConvertYamlAndProperties: IntelliJ IDEA plugin - Convert Yaml And Properties Files

机器学习——强化学习中的“策略π”的个人思考

这两天回顾了《西瓜书》中的最后一章——“强化学习”&#xff0c;但是忽然发现之前对于本章中的“策略π”的理解有些偏差&#xff0c;导致我在看值函数公式时有些看不明白。对此&#xff0c;我在网上查了一些资料&#xff0c;但是大部分人都是一笔带过&#xff0c;或者是照本…

网络安全入门教程(非常详细)从零基础入门到精通,看完这一篇你就是网络安全高手了。

关于我 我算是“入行”不久的一个新人安全工作者&#xff0c;为什么是引号呢&#xff0c;因为我是个“半个野路子”出身。早在13年的时候&#xff0c;我在初中时期就已经在90sec、wooyun等社区一直学习、报告漏洞。后来由于升学的压力&#xff0c;我逐渐淡出了安全圈子&#x…

卷烟厂打叶复烤线万亿条数据处理难?TDengine 来解决!

在当今竞争激烈的市场环境中&#xff0c;烟草行业面临着越来越多的挑战。如何提升生产效率、降低成本、实现数字化转型&#xff0c;成为各大卷烟厂亟需解决的问题。近日&#xff0c;为解决某卷烟厂面临的一系列挑战&#xff0c;TDengine与云南昆船电子设备有限公司达成签约合作…

第二节:如何使用thymeleaf渲染html(自学Spring boot 3.x的第一天)

大家好&#xff0c;我是网创有方&#xff0c;今天来学习如何使用thymeleaf渲染html。该模板运用不广泛&#xff0c;所以本节内容了解既可。 第一步&#xff1a;创建html文件。 在模板templates目录下创建一个html文件。 编写代码如下&#xff1a; <!DOCTYPE html> <…

教程:在 Kubernetes 集群上部署 WordPress 网站

WordPress 是专为每个人设计的开源软件&#xff0c;强调创建网站、博客或应用程序的可访问性、性能、安全性和易用性。WordPress 是一个基于 PHP 的内容管理系统&#xff08;CMS&#xff09;&#xff0c;使用 MySQL 作为数据存储&#xff0c;目前很多网站、电商独立站、个人博客…