您的位置:首页 > 其它

POJ-1014-Dividing

2013-10-06 18:19 260 查看
// 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: