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.
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
这道题很适合用深搜,运行时间给得很宽裕,做出正确结果的关键点在于能不能把输出顺序做好。很多人认为用深搜就可以按顺序输出,但其实并不能得到正确答案。
我写的时候用数组保存了输出顺序,避免了这个尴尬。另外AC的另一个点就是你是否注意结果要隔开一行(没发现结果提示了好多次)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Max=31;
int ASS[100100][40];
int a[Max],b[Max];
int N,num,S,OK,K,l;
void dfs(int M){
for(int i=M;i<num-2;i++){
b[N++]=a[i];S+=a[i];
for(int j=i+1;j<num-1;j++){
for(int k=j+1;k<num;k++){
if(a[k]==S+a[j])
{
OK=1;
for(int f=0;f<N;f++)
ASS[K][f+1]=b[f];
ASS[K][0]=N+1;
ASS[K][N+1]=a[j];ASS[K][N+2]=a[k];
if(N+1>l) l=N+1;
K++;
}
}
}
dfs(i+1);
S-=a[i];N--;
}
}
int main()
{
int T;
while(scanf("%d",&T)!=EOF){
while(T--){
scanf("%d",&num);
OK=0;
for(int i=0;i<num;i++)
scanf("%d",&a[i]);
sort(a,a+num);
K=0;
N=0;S=0;
dfs(0);
if(OK==0){
printf("Can't find any equations.\n");
printf("\n");
}
else{
for(int j=2;j<=l;j++){
for(int i=0;i<K;i++){
if(ASS[i][0]==j){
for(int k=1;k<=j;k++){
if(k==1) printf("%d",ASS[i][k]);
else printf("+%d",ASS[i][k]);
}
printf("=%d\n",ASS[i][j+1]);
}
}
}
printf("\n");
}
}
}
return 0;
}