深搜05(深搜+减枝+存储字符串) Additive equations

ZOJ Problem Set - 1204

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

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int num[32];
int MAX;
int n;
int has;
int vis[32];
/*
当算式左边加数的个数可能不止一个,如1+2=3 1+3=4 1+4=5 1+5=6 2+3=5 2+4=6,
也就是说,每当碰到一个 加数的个数为cnt的算式时,都让counts[cnt]++
*/
int cnt;
int counts[32];
string result[30][6000];
void clear() {//初始化工作很重要
	for(int i=0; i<32; i++) {//觉得使用memset函数存在风险,使用for循环来清零靠谱
		vis[i]=0;
		counts[i]=0;
	}
	MAX=0;
	has=0;
}
string change(int material) {//将整数转换为字符串并返回
	string str="";
	char ch[22];
	int m=0;
	while(material) {
		ch[m++]=material%10 + '0';
		material/=10;
	}
	for(int i=m-1; i>=0; i--) {
		str+=ch[i];
	}
	return str;
}
void solve(int s) {//拼接处理得到的结果
	int flag=0;
	string str="";
	for(int k=0; k<n; k++) {
		if(vis[k]) {
			if(flag==0) {
				str+=change(num[k]);
				flag=1;
			} else if(flag==1) {
				str= str+'+'+change(num[k]);
			}
		}
	}
	str=str+'='+change(s);
	//这样就可以使用result[cnt][counts[cnt]++]=str的方式把每一个 加数的个数为cnt的等式都存储起来
	result[cnt][counts[cnt]++]=str;
}
void dfs(int sum,int cur) {
	if(sum>num[n-1]) return;//如果和大于了最后一个,则可判断不符合,是减枝的一种方式
	if(!vis[cur]) {
		cnt++;//新搜寻一个加数是,就让cnt增加1
		vis[cur]=1;
		for(int i=cur+1; i<n; i++) {
			if(sum==num[i]) {
				if(cnt>MAX) MAX=cnt;//在所有的等式中,把 加数的个数最多的那个等式 的 加数的个数 记录下来
				has=1;//说明发现了equation
				solve(sum);
			}
			dfs(sum+num[i],i);//继续寻找下一个加数
		}
		cnt--;//回溯
		vis[cur]=0;
	}
}
int main() {
	//freopen("in.txt","r",stdin);
	int all;
	scanf("%d",&all);
	while(all--) {
		clear();
		scanf("%d",&n);
		for(int i=0; i<n; i++) {
			scanf("%d",&num[i]);
		}
		sort(num,num+n);//排序
		for(int i=0; i<n; i++) {
			dfs(num[i],i);
		}
		if(has) {//如果存在等式
			for(int i=2; i<=MAX; i++) {//输出结果
				for(int j=0; j<counts[i]; j++) {
					cout<<result[i][j]<<endl;
				}
			}
		} else {
			printf("Can't find any equations.\n");
		}
		printf("\n");
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值