解题思路:其实就是01背包问题
注意事项:
参考代码:
#include <deque>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <iterator>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <string>
#include <cmath>
#include <string.h>
#define V 1001
using namespace std;
unsigned int f[101][V];//全局变量,自动初始化为0
unsigned int weight[101];
unsigned int value[101];
#define max(x,y) (x)>(y)?(x):(y)
int main()
{
int N,M;
cin>>M;//背包容量
cin>>N;//物品个数
for (int i=1;i<=N; i++)
{
cin>>weight[i]>>value[i];
}
for (int i=1; i<=N; i++)
for (int j=1; j<=M; j++)
{
if (weight[i]<=j)
{
f[i][j]=max(f[i-1][j],f[i-1][j-weight[i]]+value[i]);
}
else
f[i][j]=f[i-1][j];
}
cout<<f[N][M]<<endl;//输出最优解
}
0.0分
1 人评分
C语言训练-计算一个整数N的阶乘 (C语言代码)浏览:928 |
A+B for Input-Output Practice (C++代码)浏览:605 |
上车人数 (C语言代码)浏览:777 |
printf基础练习2 (C语言代码)浏览:941 |
成绩转换 (C语言代码)浏览:1005 |
C二级辅导-阶乘数列 (C语言代码)浏览:688 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)浏览:780 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5228 |
【求[X,Y]内被除3余1并且被除5余3的整数的和】 (C语言代码)浏览:672 |
Minesweeper (C语言描述,蓝桥杯)浏览:1122 |