ZOJ1204——Additive equations(DFS)
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)的更多相关文章
- zoj 1204 Additive equations
ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...
- Additive equations--zoj
Additive equations Time Limit: 10 Seconds Memory Limit: 32768 KB We all understand that an inte ...
- ZOJ 1204 一个集合能组成多少个等式
Additive equations Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 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 ...
- How Many Equations Can You Find(dfs)
How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 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 [从零 ...
- hdu - 2266 How Many Equations Can You Find (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2266 给一个字符串和一个数n,在字符串中间可以插入+或者 -,问有多少种等于n的情况. 要注意任意两个数之间都可 ...
随机推荐
- c#基础汇总-------------封装
说到封装,其实是比较基础类的问题,它为程序设计提供了系统与系统,模块与模块,类与类之间交互的实现手段.在.Net中,一切看起来都已经被包装在.Net FrameWork这一复杂的网络中,提供给最终开发 ...
- WaitForSingleObject用法
对应函数 编辑 VC声明 DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); 参数 编辑 hHandle[in]对 ...
- DEDECMS中,arclist标签
文档列表 dede:arclist 标签: {dede:arclist flag='h' typeid='' row='' col='' titlelen='' infolen='' imgwidt ...
- 分享php中四种webservice实现的简单架构方法及实例[转载]
[转载]http://www.itokit.com/2012/0417/73615.html 本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC ...
- linux乱码问题:LANG变量的秘诀
对于国内的Linux用户,经常烦恼的一个问题是:系统常常在需要显示中文的时候却显示成了乱码,而由于某些原因,需要英文界面的系统的时候,却苦于系统不能正常输入和显示中文.另外,由于大部分主要Linux发 ...
- fedora 解决yumBackend.py进程CPU占用过高
fedora启动时电脑风扇噪声巨响,检查进行发现是yumBackend.py进行占用CPU过高. yumBackend.py进行是后台检查更新,如果觉得没用可以使用工具关闭检查更新,或者修改检查周期. ...
- Oracle RAC集群安装之:Grid软件安装过程蓝屏
前几天在安装一套RAC服务器的过程中,遇到了蓝屏事件,折腾了蛮久(在排查是否存在硬件问题上花费了相当多一部分时间),整个过程大概说明如下: 1.两台华为的PC SERVER,操作系统为WIN SERV ...
- CentOS安装配置Git服务器(gitosis)
主要参考: http://blog.csdn.net/dengjianqiang2011/article/details/9260435 辅助参考: http://freeloda.blog.51ct ...
- Spring的配置文件
Web.xml将会配置Spring的配置文件位置: <servlet> <servlet-name>x</servlet-name> & ...
- Hibernate从入门到精通(九)一对多双向关联映射
上次的博文Hibernate从入门到精通(八)一对多单向关联映射中,我们讲解了一下一对多单向映射的相关内容,这次我们讲解一下一对多双向映射的相关内容. 一对多双向关联映射 一对多双向关联映射,即在一的 ...