// Input: n1 n2 n3 n4 n5 n6\n
// max_n = 20000
// Output: Collection #1:
// Can't be divided.
// Method: calc total sum, test get half
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <math.h>
// STRUCTS
// VARS
int cntMarbles[6];
int sum;
// FUNCS
bool readLine()
{
sum = 0;
bool ok = false;
for (int i = 0; i < 6; ++i)
{
scanf("%d", cntMarbles + i);
sum += (cntMarbles[i] * (i + 1));
if (cntMarbles[i])
ok = true;
}
return ok;
}
// test get value
bool getValue(int val, int start)
{
if (val == 0)
return true;
for (int i = start; i >= 0; --i)
{
if (cntMarbles[i] > 0)
{
// judge before call
if (val - i - 1 >= 0)
{
-- cntMarbles[i];
if (getValue(val - i - 1, i))
return true;
// else not add it ??? but this can AC
}
}
}
return false;
}
// output result
void outputRet(int i, bool canDivided)
{
printf("Collection #%d:\n", i);
if (canDivided)
printf("Can");
else
printf("Can't");
printf(" be divided.\n\n");
}
void solve(int count)
{
bool canDivided = false;
if (sum % 2 == 0)
canDivided = getValue(sum / 2, 5);
outputRet(count, canDivided);
}
//#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
int count = 0;
while (readLine())
{
solve(++ count);
}
return 0;
}
POJ-1014-Dividing
最新推荐文章于 2021-10-22 21:33:34 发布