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: ...
随机推荐
- IOS中 类扩展 xib
一.类扩展(class extension,匿名分类) .格式 @interface 类名 () { // 成员变量... } // 方法声明... @end .作用 > 写在.m文件中 > ...
- 优秀个人免费私有云OwnCloud 8.0终于发布 - 亮眼新功能初探简介
OwnCloud是一个基于Linux的私有云开源项目,用户可以免费使用它来搭建自己的私有云来进行文件和多媒体等的分享. 本人有幸在大概一年多前因为项目需要而接触和使用到OwnCloud,但不幸的是当时 ...
- jQuery多文件
jQuery多文件下载 文件下载是一个Web中非常常用的功能,不过你是做内部管理系统还是做面向公众的互联网公司都会遇到这个问题,对于下载一般有点实际开发经验的都会自己解决,上周弄了一下多文件下载,业务 ...
- bluetooth发展(五岁以下儿童)------蓝牙功能测试(一个)
newton板已出版.下面再组织我调试的一小方面,,蓝牙功能的实现和测试: 转载请注明出处:http://blog.csdn.net/wang_zheng_kai 以下是我写的newton开发板中bl ...
- Oracle 11g sys,system 密码忘记设置解决办法
原文:Oracle 11g sys,system 密码忘记设置解决办法 1.启动sqlplus 2.请输入用户名: sqlplus/as sysdba 3.输入口令: 直接回车 4.连接到: Ora ...
- C语言中的内存管理
开始陆续的发一下唐老师视频的笔记吧,顺便带一些正冲哥书的的内容.不能一下都发出来,因为内容发多了自己也受不了,而且发的都是学习视频时候的一些笔记,可能会有一些问题不是很清晰. 先说一下C语言中的内存管 ...
- LINQ TO SQL ——Group by
原文:LINQ TO SQL --Group by 分组在SQL中应用的十分普遍,在查询,统计时都有可能会用到它.LINQ TO SQL中同样具备group的功能,这篇我来讲下LINQ TO SQL中 ...
- StringTokenizer使用类
StringTokenizer它被用来分离String应用类别.当量VB的split功能. 1.构造函数 public StringTokenizer(String str) public Strin ...
- 个推推送 产品SDK常见问题检查
作者:Hong Jack链接:https://zhuanlan.zhihu.com/p/20733333来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 通知和消息有什么 ...
- 求解轨道力学二体意义下的Lambert方程(兰伯特方程)的Fortran程序
轨道力学中二体问题下求解兰伯特方程. 老外写的Matlab程序,我把它转成了Fortran程序. !************************************************** ...