POJ 1014--Dividing

来源:互联网 发布:js设置文本框不可编辑 编辑:程序博客网 时间:2024/05/20 13:05

题意

题目大意是说,有若干个价值为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;}
原创粉丝点击