题意是说,给一些大理石,每种有num[i]个,判断能否分成两部分,可行性问题,感觉不像是动态规划,中间没有最优子结构这个性质,倒像是纯粹的递推
View Code
1 #include<stdio.h> 2 #include<string.h> 3 #define N 60005 4 int bag[7]; 5 int v[N]; 6 bool dp[N]; 7 int main() 8 { 9 int sum; 10 int i,j,k,ans; 11 int icase=1; 12 while(1) 13 { 14 sum=0; 15 for(i=1;i<=6;i++) 16 { 17 scanf("%d",&bag[i]); 18 sum+=bag[i]*i; 19 } 20 if(!sum) 21 break; 22 printf("Collection #%d:\n",icase++); 23 if(sum&1) 24 { 25 printf("Can't be divided.\n\n"); 26 continue; 27 } 28 memset(dp,0,sizeof(dp)); 29 ans=sum>>1; 30 k=0; 31 for(i=1;i<=6;i++) 32 { 33 j=1; 34 while(j<=bag[i]) 35 { 36 bag[i]-=j; 37 v[k++]=j*i; 38 j<<=1; 39 } 40 if(bag[i]) 41 v[k++]=bag[i]*i; 42 } 43 dp[0]=true; 44 for(i=0;i<k;i++) 45 for(j=ans;j>=v[i];j--) 46 if(dp[j-v[i]]) 47 dp[j]=true; 48 if(dp[ans]) 49 printf("Can be divided.\n\n"); 50 else 51 printf("Can't be divided.\n\n"); 52 } 53 return 0; 54 }