ZOJ-1204 Additive equations (DFS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xMaii/article/details/75647063
Additive equations

Time Limit: 10 Seconds       Memory Limit: 32768 KB

    We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an additive equation is, let's look at the following examples: 
    1+2=3 is an additive equation of the set {1,2,3}, since all the numbers that are summed up in the left-hand-side of the equation, namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3 and 2+1=3 the same equation, and will always output the numbers on the left-hand-side of the equation in ascending order. Therefore in this example, it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
    It is not guaranteed that any integer set has its only additive equation. For example, the set {1,2,5} has no addtive equation and the set {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6, etc. When the number of integers in a set gets large, it will eventually become impossible to find all the additive equations from the top of our minds -- unless you are John von Neumann maybe. So we need you to program the computer to solve this problem.

Input

The input data consists of several test cases. 
The first line of the input will contain an integer N, which is the number of test cases. 
Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.

Output

For each test case, you are supposed to output all the additive equations of the set. These equations will be sorted according to their lengths first( i.e, the number of integer being summed), and then the equations with the same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can't find any equations." in a line. Print a blank line after each test case.

Sample Input
3
3 1 2 3
3 1 2 5
6 1 2 3 5 4 6

Output for the Sample Input

1+2=3

Can't find any equations.

1+2=3
1+3=4
1+4=5
1+5=6
2+3=5
2+4=6
1+2+3=6

 


Source: Zhejiang University Local Contest 2002, Preliminary


题目链接


题意是给定一个正整数集合,利用其集合元素打印出所有存在的和式,若无则输出"Can't find any equations."。而且是按照等式的长度从小到大的输出(等式的长度即加数的个数)。其次,对于长度相等的等式,则按照字典序从小到大来输出。我们可以根据等式的长度,依次对集合内的每一个数进行深度搜索,找到符合题意的等式。

代码如下。

扫描二维码关注公众号,回复: 3771173 查看本文章
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 31;

int set[N], vis[N];
int T, M;
bool flag;

void dfs(int length, int start, int sum)
{
	if (length == 0)	//等式组建完毕
	{
		for (int i = start; i < M&&sum >= set[i]; i++)	//搜索等式的和sum是否在集合中
		{
			if (sum == set[i])
			{
				flag = true;
				for (int j = 0; j <= i && sum; j++)		//输出等式,即去寻找vis[j]==1的加数
				{
					if (vis[j])
					{
						if (sum != set[j])
							printf("%d+", set[j]);
						else if (sum == set[j])			//此时set[j]是最后一个加数
							printf("%d=%d\n", set[j], set[i]);
						sum -= set[j];
					}
				}
			}
		}
		return;
	}

	for (int i = start; i < M; i++)		
	{
		if (sum + set[i] <= set[M - 1])	//如果和值比最后一个数还大,则舍弃
		{
			vis[i] = 1;
			dfs(length - 1, i + 1, sum + set[i]);
			vis[i] = 0;
		}
	}
}

bool solve()
{
	flag = false;
	for (int length = 2; length < M; length++) //搜索长度是length的等式
	{
		memset(vis, 0, sizeof(vis));
		dfs(length, 0, 0);
	}
	return flag;
}

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &M);
		for (int i = 0; i < M; i++) scanf("%d", &set[i]);
		sort(set, set + M);		//将数组set按照从小到大的顺序排序
		if (!solve())
			printf("Can't find any equations.\n");
		printf("\n");
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/xMaii/article/details/75647063