Additive equations

Description
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

题目大意:

    给定一个数列 找出其中的加法等式x1+x2+x3+..xn=y(其中x1,x2,x3,xn,y属于数列) (n>=2)

解题思路:

    可以将数列看做一个无向完全图(即每个顶点都指向其他所有顶点)。用DFS搜索,将符合题目要求的存起来,再排序输出即可。 具体细节请看代码(语死早,没办法^^)

    细节:

      1)符合要求的等式比能保证x1<x2<x3<<xn<y

      2)搜索前排好序,由小到大。根据1)可知只搜素比当前元素靠后的元素是否等于当前递归的总和即可。

Code:

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#define MAXN 30000
using namespace std;
int a[],flag[];
int N;
struct s //用于存等式用的结构体,记录等式中的各个元素和元素个数(最后一个元素必为等号右边元素)
{
int a[];
int lenth;
} str[MAXN];
int k=;
void output() //输出函数,k表示搜索后的符合要求的等式的数量。
{
if (k==) printf("Can't find any equations.\n\n");
else
{
for (int i=; i<=k; i++)
{
int j;
printf("%d",str[i].a[]);
for (j=; j<str[i].lenth; j++)
printf("+%d",str[i].a[j]);
printf("=%d\n",str[i].a[j]);
}
printf("\n");
}
}
void input()//DFS到符合要求的等式,将等式的各个元素存入数组
{
k++;
str[k].lenth=;
int t=;
for (int i=; i<=N; i++)
if (flag[i]!=) str[k].a[t++]=a[i],str[k].lenth++;
}
void DFS(int i,int sum) //flag[i]==1表示当前DFS中 i被使用了。
{ //当递归到符合条件的时候可根据当前的flag数组情况来获取等式的相关元素
sum+=a[i];
if (sum>a[N]) return ;
for (int j=i+; j<=N; j++)
if (sum==a[j])
{
flag[j]=;
input();
flag[j]=;
}
for (int j=i+; j<=N; j++)
{
flag[j]=;
DFS(j,sum);
flag[j]=;
}
}
bool cmp(struct s a,struct s b)//根据题目要求排序,保证短的在前,相同长度情况下数字小的在前
{
if (a.lenth!=b.lenth) return a.lenth<b.lenth;
for (int i=; i<=a.lenth; i++)
if (a.a[i]!=b.a[i]) return a.a[i]<b.a[i];
}
int main()
{
int T;
cin>>T;
while (T--)
{
memset(flag,,sizeof(flag));
memset(a,,sizeof(a));
k=;
cin>>N;
for (int i=; i<=N; i++)
cin>>a[i];
sort(a+,a++N);
for (int i=; i<=N; i++)
{
flag[i]=;
DFS(i,);
flag[i]=;
}
sort(str+,str+k+,cmp);
output();
}
return ;
}

ZOJ1204——Additive equations(DFS)的更多相关文章

  1. zoj 1204 Additive equations

    ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...

  2. Additive equations--zoj

    Additive equations Time Limit: 10 Seconds      Memory Limit: 32768 KB We all understand that an inte ...

  3. ZOJ 1204 一个集合能组成多少个等式

    Additive equations Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other ...

  4. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  5. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  6. HDU 2266 How Many Equations Can You Find(DFS)

    How Many Equations Can You Find Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. How Many Equations Can You Find(dfs)

    How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  8. HDOJ(HDU).2266 How Many Equations Can You Find (DFS)

    HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...

  9. hdu - 2266 How Many Equations Can You Find (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可 ...

随机推荐

  1. a2x

    #include <typeinfo> template <typename T> bool a2x( T& _f , char* p) { if( !p ) retu ...

  2. [Guava学习笔记]Collections: 集合工具类

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3861431.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  3. Windows Phone 为指定容器内的元素设置样式

    在Windows Phone中设置元素样式有多种 拿TextBlock来说 1.我们可以直接在控件上设置: <TextBlock Text="自身样式设置" Width=&q ...

  4. [转]Nuget挂了的解决方法

    今天用Nuget下一个程序包时,发现Nuget挂了:未能解析此远程名称:'nuget.org'.第一反应就是方校长抖威风了,挂个代理上 http://nuget.org 试了下,果然好好的. 用命令n ...

  5. mysql查询区分大小写与自定义排序

    mysql查询区分大小写: SELECT id,developer FROM products WHERE developer != '' and developer = binary('LYNN') ...

  6. Java中的堆内存、栈内存、静态存储区

    一.栈 栈的优势是,存取速度比堆要快,仅次于直接位于CPU中的寄存器,当超过变量的作用域后,java会自动释放掉为该变量分配的内存空间,该内存空间可以立刻被另作他用.但缺点是,存在栈中的数据大小与生存 ...

  7. input属性disabled和readonly的区别

    两种属性的写法如下: 1.<input type="text" name="name" value="xxx" disabled=&q ...

  8. spring-cloud-bus

    安装rabbitmq 依赖erlang: http://erlang.org/download/otp_win64_18.2.exe

  9. PHP SQL注入的防范

    说到网站安全就不得不提到SQL注入(SQL Injection),如果你用过ASP,对SQL注入一定有比较深的理解,PHP的安全性相对较高,这是因为MYSQL4以下的版本不支持子语句,而且当php.i ...

  10. centos 安装ecshop出现date错误

    centos 安装ecshop 错误提示 Warning: date(): It is not safe to rely on the system's timezone settings. You ...