hdoj 1248(背包问题)(动态规划)

来源:互联网 发布:如何做网络写手 编辑:程序博客网 时间:2024/06/08 18:03
#include<iostream>#include<stdio.h>#include<string>using namespace std;#define MAX  10050int collect[MAX];int weight[3]={150,200,350};int limate;int dp(int p){if(p==limate){collect[p]=p;return collect[p];}//如果访问过if(collect[p]!=-1){return collect[p];}//访问int cs;int tmax=-1;int i;for(i=0;i<3;i++){cs=p+weight[i];if(cs<=limate){int fmax=dp(cs);if(fmax>tmax){tmax=fmax;}}}collect[p]=tmax;//如果什么都没有选择if(collect[p]==-1){collect[p]=p;}return collect[p];}void init(){int i;for(i=0;i<MAX;i++){collect[i]=-1;}}int main(){//freopen("input.txt","r",stdin);int T;scanf("%d",&T);getchar();while(T--){init();scanf("%d",&limate);getchar();int p=0;dp(p);cout<<limate-collect[0]<<endl;}return 0;}

原创粉丝点击