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. 使用JavaScript实现简单的输入校验

    HTML页面代码: <!doctype html> <html lang="en"> <head> <meta charset=" ...

  2. SQL语句基本概念

    星期五 2014年11月21日 SQL语句:     sql语句用于检索维护数据库中的数据                   [ create alter Select update        ...

  3. hash桶

    #include <stdio.h> #include <stdlib.h> #include "chain.c" //include the chain. ...

  4. SMB/CIFS协议解析二

    一.拷贝文件(远程-->本地) 1.SMB_COM_NT_CREATE_ANDX (0xa2)       打开文件,获取文件名,获得读取文件的  总长度. 2.SMB_COM_READ     ...

  5. linux find 反转 查找没有被找到的结果

    在linux下,有时候需要找一些文件,还有时候这些文件格式不够统一和规范,但是需要排除的那些文件却格式统一,就可以使用find命令的反转功能 一般用find查找文件的命令是: find . -name ...

  6. 漫谈php全局变量Global

    global语句的作用是定义全局变量,例如如果想在函数内访问全局作用域内的变量则可以通过global声明来定义. 下面从语法解释开始分析. 1. 词法解析 查看 Zend/zend_language_ ...

  7. javascript dom追加内容的例子

    javascript dom追加内容的使用还是比较广泛的,在本文将为大家介绍下具体的使用方法. 例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  8. NotifyIcon制作任务栏托盘菜单

    常用软件飞信.QQ在任务栏中的图标ICO,以及鼠标移动到图标是右键菜单选项 1.首先制作任务栏图标 this.ShowInTaskbar = true; 2.窗体最小化时或者关闭时隐藏到任务栏,有时候 ...

  9. "!x++" 我之见解

    "!x++"之说,各人见解不同,但真理只有一个.我只尝试着说出一种见解,未知真相. 何如? "!x++"等价于"!(x++)". 理论分析 ...

  10. java、js的编码、解码

    如果在地址栏挂载参数,特别是包含中文,往往要进行编码,取值时再解码,以下是java和js中编码.解码的各自方法. java: @Test public void test3() throws Unsu ...