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
算法分析:
一、 对读入的数据进行处理:
1. 排序 sort()函数
2. 使用哈希表,判断相加结果是否存在
二、输出
1. 使用栈path记录
2. 使用flag标记是否存在Additive equations,如果flag = -1,表示不存在
const int maxm = 40;
const int maxn = 1e5 + 10;
int a[maxm];
int path[maxm];
int h[maxn];
int top = -1;
int flag = -1;
int n, m; //测试次数和数组元素个数
flag = -1;
memset(h, -1, sizeof(h));
scanf("%d", &m);
for (int i = 1; i <= m; i++) //读取m个数据
{
scanf("%d", a + i);
h[a[i]] = a[i];
}
sort(a + 1, a + m + 1); //排序
三、过程
1. 从2个数相加开始,到3个数相加,逐个递增,最多m-1个数相加
2. cnt个数相加,起始位置是1,从1到m-2,如果(起始位置+cnt>m)表示这个数处理完毕
3. dfs(起始位置,已经相加的数的个数,最大需要相加个数,和)。
for (int cnt = 2; cnt <= m - 1; cnt++)
{
for (int s = 1; s <= m; s++)
{
if (s + cnt > m)
break;
int sum = a[s];
top = -1;
path[++top] = a[s];
dfs(s + 1, 1, cnt, sum);
}
}
4. dfs处理过程
void dfs(int start, int cnt, int max_cnt, int sum)
{
if (sum > a[m])
return;
if (cnt == max_cnt)
{
if (h[sum]!=-1)
{
flag = 1;
printf("%d", path[0]);
for (int i = 1; i <= top; i++)
printf("+%d", path[i]);
printf("=%d\n", sum);
}
return;
}
for (int i = start; i <= m; i++)
{
sum += a[i];
path[++top] = a[i];
dfs(i + 1, cnt + 1, max_cnt, sum);
sum -= a[i];
top--;
}
}
四、代码如下
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
const int maxm = 40;
const int maxn = 1e5 + 10;
int a[maxm];
int path[maxm];
int h[maxn];
int top = -1;
int flag = -1;
int n, m; //测试次数和数组元素个数
void dfs(int start, int cnt, int max_cnt, int sum)
{
if (sum > a[m])
return;
if (cnt == max_cnt)
{
if (h[sum]!=-1)
{
flag = 1;
printf("%d", path[0]);
for (int i = 1; i <= top; i++)
printf("+%d", path[i]);
printf("=%d\n", sum);
}
return;
}
for (int i = start; i <= m; i++)
{
sum += a[i];
path[++top] = a[i];
dfs(i + 1, cnt + 1, max_cnt, sum);
sum -= a[i];
top--;
}
}
int main()
{
scanf("%d", &n); //测试次数
while (n--)
{
flag = -1;
memset(h, -1, sizeof(h));
scanf("%d", &m);
for (int i = 1; i <= m; i++) //读取m个数据
{
scanf("%d", a + i);
h[a[i]] = a[i];
}
sort(a + 1, a + m + 1); //排序
for (int cnt = 2; cnt <= m - 1; cnt++)
{
for (int s = 1; s <= m; s++)
{
if (s + cnt > m)
break;
int sum = a[s];
top = -1;
path[++top] = a[s];
dfs(s + 1, 1, cnt, sum);
}
}
if (flag == -1)
printf("Can't find any equations.\n");
printf("\n");
}
return 0;
}