Chinaunix首页 | 论坛 | 博客
  • 博客访问: 436455
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-03-31 23:52:02

#include <stdio.h>
#include <string.h>

int dp[120001];
int c[7];
int sum, divi;
static int solve(void);
int main(int argc, char **argv)
{
        int cases = 0, i;
        while(1){
                cases++;
                sum = divi = 0;
                memset(dp, 0, sizeof(dp));
                for(i = 1; i <= 6; i++){
                        scanf("%d", &c[i]);
                        sum += c[i] * i;
                }
                getchar();
                if(sum == 0){
                        break;
                }
                else if(sum%2 == 0){
                        sum /= 2;
                        divi = solve();
                }
                printf("Collection #%d:\n", cases);
                if(divi == 1){
                        printf("Can be divided.\n");
                }
                else if(divi == 0){
                        printf("Can't be divided.\n");
                }
                printf("\n");
        }
        return 0;
}

static int solve(void)
{
        int i, j, k, temp, max = 0;

        dp[0] = 1;
        for(i = 1; i <= 6; i++){
                if(c[i]){
                        for(j = max; j >= 0; j--){
                                if(dp[j]){
                                        temp = j;
                                        for(k = 1; k <= c[i]; k++){
                                                temp += i;
                                                dp[temp] = 1;
                                                if(temp == sum){
                                                        return 1;
                                                }
                                                if(temp > sum){
                                                        break;
                                                }
                                        }
                                        if(temp > max){
                                                max = temp;
                                        }
                                        if(max > sum){
                                                max = sum;
                                        }
                                }
                        }
                }
        }
        return 0;
}

阅读(739) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~