今天开始每天10行代码

来源:互联网 发布:下载长城证券软件 编辑:程序博客网 时间:2024/06/14 08:31
#include <iostream>
#include <algorithm>
#include<stdio.h>
using namespace std;
#define MAX_N 100


int n,W;
int w[MAX_N],v[MAX_N];
//从第i个物品挑选总重量小于j的部分
int rec(int i,int j){
int res;
if(i==n){
res=0;
}
else if (j<w[i]){
res=rec(i+1,j);
}else {
res=max(rec(i+1,j),v[i]+rec(i+1,j-w[i]));
}
return res;
}
int  main (){
scanf("%d%d", &n, &W);
for(int i=0;i<n;i++){
scanf("%d%d", &w[i], &v[i]);
}
printf("%d\n", rec(0,W));

}

挑战程序代码,由记忆搜索引出动态规划,揭露dp本质

0 0
原创粉丝点击