思路:简单的动态规划,0-1背包问题
#include #include int a[1001][1001],va[1001],vo[1001];int max(int x, int y){ return x>y?x:y;}int main(){ int T,i,j,n,v; scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); scanf("%d%d",&n,&v); for(i = 0;i < n;i ++) scanf("%d",&va[i]); for(i = 0;i < n;i ++) scanf("%d",&vo[i]); for(i = 0;i <= v;i ++) { for(j = 0;j < n;j ++) { if(i>=vo[j]) a[i][j] = max(a[i][j-1],a[i-vo[j]][j-1]+va[j]); else a[i][j] = a[i][j-1]; } } printf("%d\n",a[v][n-1]); }}
#include #include int a[1001],va[1001],vo[1001];int max(int x, int y){ return x>y?x:y;}int main(){ int T,i,j,n,v; scanf("%d",&T); while(T--) { memset(a,0,sizeof(a)); scanf("%d%d",&n,&v); for(i = 1;i <= n;i ++) scanf("%d",&va[i]); for(i = 1;i <= n;i ++) scanf("%d",&vo[i]); for(i = 1;i <= n;i ++) { for(j = v;j >= 0;j --) { if(j>=vo[i]) a[j] = max(a[j],a[j-vo[i]]+va[i]); else a[j] = a[j]; } } printf("%d\n",a[v]); }}