二位數組做法;
#include#include #define M 1009 typedef struct pack { int cost; int val; }PACK; int f[M][M]; #include#include #define M 1009 typedef struct pack { int cost; int val; }PACK; int main() { int cas,n,v,i,j; int f[M]; PACK a[M]; scanf("%d",&cas); while(cas--){ scanf("%d%d",&n,&v); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) scanf("%d",&a[i].val); for(i=1;i<=n;i++) scanf("%d",&a[i].cost); for(i=1;i<=n;i++) for(j=v;j>=a[i].cost;j--) if(f[j] int main(){ int cas,n,v,i,j; PACK a[M]; scanf("%d",&cas); while(cas--){ scanf("%d%d",&n,&v); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) scanf("%d",&a[i].val); for(i=1;i<=n;i++) scanf("%d",&a[i].cost); for(i=1;i<=n;i++) for(j=0;j<=v;j++) if(j-a[i].cost>=0 && f[i-1][j] < f[i-1][j-a[i].cost]+a[i].val) f[i][j]=f[i-1][j-a[i].cost] + a[i].val; else f[i][j]=f[i-1][j]; printf("%d\n",f[n][v]); } return 0;}
一位數組: