ZOJ 1204 一个集合能组成多少个等式
2013-07-19 18:35
267 查看
Additive equations
Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 22 Accepted Submission(s) : 12
[align=left]Problem Description[/align]
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到N的连续整数组成的集合划分成两个子集合)
- 给定一个集合A=[0,1,3,8](该集合中的元素都是在0,9之间的数字,但未必全部包含), 指定任意一个正整数K,请用A中的元素组成一个大于K的最小正整数。
- [LintCode 382]给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形?
- 给出一个set的字符和一个正数k,求所有由这个set能组成长度为k的字符串集合 print-all-combinations-of-given-length
- HDU 5441 Travel(求各个集合里的秩能组合多少对,只能在一个集合里组合)
- 一个有N个元素的集合,其中有相同元素。 需要得到按重复元素多少排序的新集合。
- JS 将一个数组按指定长度分割,组成一个新的数组集合
- 给定正整数n和m,计算出n个元素的集合{1,2,...,n}可以划分为多少个不同的由m个元素组成的子集合
- hdu 1358 Period(给定一个字符串,求有多少个前缀(包括自己本身),它是由k(k>2,并且尽量大)个循环节组成的)
- 一个七位数只能由1,2,3,4,5,6,7组成的,不能重复,从大到小求第2014个数是多少
- 转载: 查看HADOOP中一个文件有多少块组成及所在机器ip
- java如何判断一个String类型数据由多少字节组成?
- codeforce之最少加多少能组成一个凸三角形
- zoj 3511 Cake Robbery 给一个N个点的环(就当它是个正多边形好了),每次沿着两个点切下去一刀,一共切M刀,刀与刀之间不会交叉,问切完以后边长最多的多边形的边长是多少
- 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形?
- 考虑一个特殊的hash函数h,能将任一字符串hash成一个整数k,其概率p(k) = 2^(-k),k = 1,2,3,4,....对于一个未知大小的字符串集合S中的每一个元素取hash值所组成的集
- (用树结构支持并查集8.2.2)POJ 1988 Cube Stacking(并查集的使用:判断一个集合中元素的个数---->>某一个元素的下面还有多少个元素)
- 查看HADOOP中一个文件有多少块组成
- 有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n