POJ 1014--Dividing

题意

题目大意是说,有若干个价值为1,2,3,4,5,6的大理石,问能否将这些大理石分成两份,让其总价值相等,大理石不可拆。

分析

直接深度搜索即可。
代码如下:
Memory: 692K Time: 0MS Length:31LINES

#include<iostream>
using namespace std;
bool Recurring(const int sum, int remainder, int start, int* marbles, int* array)
{
    if (remainder == sum / 2)       return true;
    for (int i = start; i >= 0; --i)
    {
        if (array[i] < marbles[i] && remainder - i - 1 >= sum / 2)
        {
            ++array[i];
            if (Recurring(sum, remainder - i - 1, i, marbles, array))       return true;
        }
    }
    return false;
}
int main()
{
    int marbles[6];
    int count = 0;
    while (cin >> marbles[0] >> marbles[1] >> marbles[2] >> marbles[3] >> marbles[4] >> marbles[5])
    {
        ++count;
        int sum = marbles[0] * 1 + marbles[1] * 2 + marbles[2] * 3 + marbles[3] * 4 + marbles[4] * 5 + marbles[5] * 6;
        if (sum == 0)   break;
        int array[6] = { 0 };
        if (sum % 2 == 0 && Recurring(sum, sum, 5, marbles, array))
            cout << "Collection #" << count << ":" << endl << "Can be divided." << endl << endl;
        else        cout << "Collection #" << count << ":" << endl << "Can't be divided." << endl << endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值