ZOJ 1204 一个集合能组成多少个等式
Additive equations
Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22 Accepted Submission(s) : 12
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
#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 一个集合能组成多少个等式的更多相关文章
- WorkFlow WF如何为一个集合赋值
今天刚刚开始学习WorkFlow.无奈WF网络上的学习资料实在太少. 刚刚学到Foreach控制流的使用,需要一个集合参数.经研究,静态赋值可以搞定.动态赋值还没. 首先添加一个List<int ...
- PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?
首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...
- 将DataTable 存到一个集合当中
将DataTable 存到一个集合中 此做法来自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTabl ...
- 5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口
1 固定集合 固定集合值得是事先创建并且大小固定的集合 2 固定集合的特征:固定集合非常像环形队列.假设空间不足,最早文档就会被删除,为新的文档腾出空间.一般来说.固定集合适用于不论什么想要自己 ...
- hibernate,createCriteria in条件 是一个集合。list 或 数组等
hibernate,createCriteria in条件 是一个集合.list 或 数组等 cq.in("states", new String[]{"2", ...
- <c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历
<c:forEach>可以默认的把以逗号分隔的字符串作为一个集合来遍历
- python 实现求一个集合的子集
概要 今天偶然看到有个关于数学中集合的问题,就突发奇想的想用python实现下求一个集合的子集. 准备 我当然先要复习下,什么是集合,什么是子集? 比较粗犷的讲法,集合就是一堆确定的东西,细致一点的讲 ...
- 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。
package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...
- 求一个集合的所有真子集 Python
给定一个集合,元素均为正整数且不重复,求该集合的所有子集 # -*- coding: utf-8 -*- """ Created on Tue Oct 10 09:04: ...
随机推荐
- 分布式消息系统kafka
kafka:一个分布式消息系统 1.背景 最近因为工作需要,调研了追求高吞吐的轻量级消息系统Kafka,打算替换掉线上运行的ActiveMQ,主要是因为明年的预算日流量有十亿,而ActiveMQ的分布 ...
- jQuery EasyUI API - Grid - DataGrid [原创汉化官方API]
最近在学习jQuery EasyUI,发现中文的文档好少,部分文档不错但它是鸟语的,为了大家也为了自己学习吧,汉化做一下笔记. 有没有说清楚的,或者翻译不正确的地方还请大家谅解指出.. 由于工作时间原 ...
- Scala Web 框架——Lift(一)准备工作
[Lift]Scala Web 框架——Lift(一)准备工作 Lift 官方网站:http://liftweb.net/ 下载 http://liftweb.net/download 下载.zip压 ...
- 【PLSQL】过程procedure形参和参数
************************************************************************ ****原文:blog.csdn.net/clar ...
- 激活windows server 2012 R2的方法
首先登陆dreamspark 注册一个账号https://www.dreamspark.com/Account/SignIn.aspx, 点击创建账户,信息可以完全瞎编,邮箱不存在也可以.然后使用“我 ...
- Android项目---LayoutParams
LinearLayout.LayoutParams extends ViewGroup.MarginLayoutParams java.lang.Object ↳ android.view.Vi ...
- php+ajax+json
来个例子:(json.html) <html lang="en"> <head> <meta charset="UTF-8"> ...
- String.Join的实现
String.Join的实现 在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的 ...
- Javascript多线程引擎(五)
Javascript多线程引擎(五)之异常处理 C语言没有提供一个像Java一样的异常处理机制, 这就带来了一个问题, 对于一个子函数中发生异常后, 需要在父函数调用子函数的位置进行Check, 如果 ...
- vector cin
vector<int>vec1,vec2; int ival; cout<<"Ender numbers for vector1(-1 to end):"& ...