Additive equations

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 12
Problem 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
 
Source
Zhejiang University Local Contest 2002, Preliminary
 
题意:  输入一个集合 看这个集合中能组成多少个等式    注意1+2=3和2+1=3一样   
另外输出首先按照参与元素个数多少排序    如果参与个数相同 则按照元素大小排序 如上面样例
 
思路:
DFS 
DFS 搜索
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
int st[100],n,vis[100];
string str;
string ans[33][6000];
int a[33];
int cnt=0;
void change(int mid)
{
int num=0;
char ss[22];
while(mid)
{
ss[num++]=mid%10+'0';
mid=mid/10;
}
for(int i=num-1;i>=0;i--) str+=ss[i];
}
void out()
{
int i,flag=1;
cnt=0;
str.clear();
for(i=0;i<n;i++)
{
if(vis[i])
{
if(flag==0)
str+='+';
flag=0;
change(st[i]);
cnt++;
}
}
}
void DFS(int sum,int pos)
{
int i;
if(sum>st[n-1]) return ;
for(i=pos+1;i<n;i++)
{
if(sum==st[i])
{
out();
str+='=';
change(st[i]);
cnt++;
ans[cnt][a[cnt]++]=str;
}
vis[i]=1;
DFS(sum+st[i],i);
vis[i]=0;
}
}
int main()
{
int cas,i;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&st[i]);
sort(st,st+n);
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
{
vis[i]=1;
DFS(st[i],i);
vis[i]=0;
}
int ok=0;
for(i=3;i<33;i++)
{
for(int j=0;j<a[i];j++)
{
ok=1;
printf("%s\n",ans[i][j].c_str());
}
}
if(ok==0)
{
printf("Can't find any equations.\n");
}
printf("\n");
}
return 0;
}

ZOJ 1204 一个集合能组成多少个等式的更多相关文章

  1. WorkFlow WF如何为一个集合赋值

    今天刚刚开始学习WorkFlow.无奈WF网络上的学习资料实在太少. 刚刚学到Foreach控制流的使用,需要一个集合参数.经研究,静态赋值可以搞定.动态赋值还没. 首先添加一个List<int ...

  2. PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?

    首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...

  3. 将DataTable 存到一个集合当中

    将DataTable 存到一个集合中 此做法来自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTabl ...

  4. 5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口

     1 固定集合 固定集合值得是事先创建并且大小固定的集合 2 固定集合的特征:固定集合非常像环形队列.假设空间不足,最早文档就会被删除,为新的文档腾出空间.一般来说.固定集合适用于不论什么想要自己 ...

  5. hibernate,createCriteria in条件 是一个集合。list 或 数组等

    hibernate,createCriteria in条件 是一个集合.list 或 数组等 cq.in("states", new String[]{"2", ...

  6. <c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历

    <c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历

  7. python 实现求一个集合的子集

    概要 今天偶然看到有个关于数学中集合的问题,就突发奇想的想用python实现下求一个集合的子集. 准备 我当然先要复习下,什么是集合,什么是子集? 比较粗犷的讲法,集合就是一堆确定的东西,细致一点的讲 ...

  8. 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。

    package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

  9. 求一个集合的所有真子集 Python

    给定一个集合,元素均为正整数且不重复,求该集合的所有子集 # -*- coding: utf-8 -*- """ Created on Tue Oct 10 09:04: ...

随机推荐

  1. Cocos2d-x 3.0 编译出错 解决 error: expected &#39;;&#39; at end of member declaration

    近期把项目移植到cocos2d-x 3.0,在整Android编译环境的时候,出现一大堆的编译出错,都是类似"error: expected ';' at end of member dec ...

  2. C语言得到当前系统时间

    void getTime(){ //获取当前系统时间 time_t tTime;//距离1900年1月1日的秒数 char str[80]; struct tm* stTim;//时间结构 time( ...

  3. asp.net中TextBox里面实现回车触发指定事件

    原文:asp.net中TextBox里面实现回车触发指定事件 我在一个user_top用户控件里面做了个包括搜索的功能.然后再一个页面中添加这个用户控件.浏览时候在textbox里面输入搜索内容后.下 ...

  4. canvas绘制自定义的曲线,以椭圆为例,通俗易懂,童叟无欺

    本篇文章,将讲述如何通过自定义的曲线函数,使用canvas的方式进行曲线的绘制. 为了通俗易懂,将以大家熟悉的椭圆曲线为例,进行椭圆的绘制.至于其他比较复杂的曲线,用户只需通过数学方式建立起曲线函数, ...

  5. APP-随身听

    简单到复杂听你的专属音响界,听金融.听物业,听新闻和其他节目专辑,简要介绍了新的音频应用,给你不一样的聆听体验.还记得老歌做?这里有.您留声机的一部分!很简单的音频应用,随时随地与此应用程序来听你的私 ...

  6. [探索]点点轻博客搬家到WordPress(一)

    摘要:点点博客备份XML通过DiandianToWordpress-beta.sh(文末给出)搬家到Wordpress博客 本人曾使用过点点轻博客,也深知像点点博客,Lofter博客导出的XML文件不 ...

  7. openwrt_git_pull命令提示merger冲突时如何解决?

    直接贴代码 tf@ubuntu:~/projects/openwrt1407$ git pull Updating 331ecb0..d12dc6e error: Your local changes ...

  8. Linux文件系统文件大小限制

    去阿里面试,被问到了Linux文件系统中文件大小的限制,当时就无语了.学操作系统时这些都是现算的,回来赶紧查一下. 以下为红帽ext3文件系统所支持的max file size和max filesys ...

  9. leetcode第七题--Reverse Integer

    Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  10. 【AngularJS】 2.0 版本发布

    [AngularJS] 2.0 版本发布 w5cValidator[AngularJS] 2.0 版本发布   w5cValidator 插件基于angular原有的表单验证,在原有的基础上扩展了一些 ...