A
class Solution
{
private:
int M;
void select(vector<int> &v, vector<int> &g, int V, int now_num, int now_v, int sum_g)
{
int all_num = v.size();
if (now_num >= all_num)
{
if (now_v == V)
{
M = max(M, sum_g);
}
return;
}
if (now_v > V)
return;
select(v, g, V, now_num + 1, now_v + v[now_num], sum_g + g[now_num]);
select(v, g, V, now_num + 1, now_v, sum_g);
return;
}
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 返回总体积为V若干物品的最大总重量,如果g存在选择若干物品总体积为V的情况,返回-1
* @param v int整型vector
* @param g int整型vector
* @param V int整型
* @return int整型
*/
int Maximumweight(vector<int> &v, vector<int> &g, int V)
{
// write code hereF
int n = v.size();
M = -1;
select(v, g, V, 0, 0, 0);
return M;
}
};
全部评论
(4) 回帖