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. c#基础学习汇总----------继承

    封装,继承,多态.这是面向对象的思想,也可以说是最基本的东西.说到继承,直接的说他就是面向对象中类与类之间的一种关系.通过继承,使得子类具有父类公有的受保护访问权限的属性和方法,同时子类可以通过加入新 ...

  2. TIMAC 学习笔记(二)

    昨天大体上熟悉了TIMAC自带的CC2530的示范例程,今天先从演示抓包入手,分析四种不同的配置工程在空中传输的差异.随后,会按照扫描.组网.入网等MAC层接口函数入手,结合IEEE 802.15.4 ...

  3. rabbitmq+haproxy+keepalived实现高可用集群搭建

    项目需要搭建rabbitmq的高可用集群,最近在学习搭建过程,在这里记录下可以跟大家一起互相交流(这里只是记录了学习之后自己的搭建过程,许多原理的东西没有细说). 搭建环境 CentOS7 64位 R ...

  4. 使用JPA TOOLS从数据库生成Entity文件

    数据库设计好后,需要生成对应的Entity文件,这是一项不怎么需要动脑筋的工作,最好的方法是交给工具完成,手工操作很容易写错或者遗漏.这里选择的工具就是JPA TOOLS. (1)先选中工程,查看右键 ...

  5. Spring多数据源的动态切换

    Spring多数据源的动态切换 目前很多项目中只能配置单个数据源,那么如果有多个数据源肿么办?Spring提供了一个抽象类AbstractRoutingDataSource,为我们很方便的解决了这个问 ...

  6. HTML5如何重塑O2O用户体验

    低频次垂直O2O服务应该继续开发原生APP吗?大家有没有发现做一个APP的推广成本和获取用户的成本越来越高?第二,用户安装APP之后,用户并不是经常点击使用APP的,那这是为什么?数据表明90%的O2 ...

  7. 为 Web 设计师准备的 25+ 款扁平 UI 工具包

    Flat UI Kit by Riki Tanone (free) Flat UI Kit (PSD) by Devin Schulz (free) Eerste UI Kit (free) Metr ...

  8. s3c-u-boot-1.1.6源码分析之一start.s

    定位到\s3c-u-boot-1.1.6\cpu\s3c64xx\start.s,打开该文件 /* * armboot - Startup Code for S3C6400/ARM1176 CPU-c ...

  9. 监听EditText

    0.得到焦点的时候,作一些处理 public class AbcActivity extends Activity implements OnFocusChangeListener{ @Overrid ...

  10. 论Oracle字符集“转码”过程

    本文将通过实验来演示一下Oracle字符集“转码”的确认过程. 1.实验环境说明 客户端是Windows XP操作系统的SQL*Plus程序,客户端字符集是936(对应Oracle的ZHS16GBK字 ...