1,问题 2,代码 /* * @Author: HerrDing (1103037805@qq.com) * @Date: 2022-05-01 23:19:31 * @LastEditors: HerrDing * @LastEditTime: 2022-05-02 00:01:29 * @FilePath: /刷题/package.cpp * @Description: how much can a bag hold * * Copyright (c) 2022 by Ding, All Rights Reserved. */ #include <iostream> #include <algorithm> using namespace std; class Package { public: float Process(const float values[], const float weights[], float alreadyweight, int i, float bag_capacity) { if (alreadyweight > bag_capacity) { return 0; } // if (i == sizeof(values) / sizeof(values[0])) 形参数组的传过来的变量名自动退化成指针,sizeof(ptr) != sizeof(values) // 具体参考csdn 收藏,最好用vector。 if (i == 3) { return 0; // termianation, no choice. } // choose or abandon i return max(values[i] + Process(values, weights, alreadyweight + weights[i], i + 1, bag_capacity), Process(values, weights, alreadyweight, i + 1, bag_capacity)); } }; int main() { Package test; float values[] = {0, 2, 4}; float weights[] = {3, 2, 1}; float bag_capacity = 1; cout << test.Process(values, weights, 0, 0, bag_capacity) << endl; }