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.
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; }
相关文章
- Set集合
- 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)
- java基础—自定义一个比较器,按照字符串的长度升序的方法来比较字符串进行储存(java集合三)
- java基础—自定义一个比较器,对TreeSet 集合中的元素按指定方法来排序(java集合六)
- Google Earth Engine ——Landsat 8 影像集合Collection详细介绍
- Google Earth Engine ——MACAv2-METDATA 数据集涵盖美国本土的 20 个全球气候模型的集合
- Google Earth Engine(GEE)——创建不同等级道路一个特征集合
- C++STL之集合set
- 20一个自定义集合的自述
- Wifitap是一个WiFi注入工具集常用命令集合大学霸IT达人
- 《Redis入门指南(第2版)》一3.6 有序集合类型
- JDK集合源码之HashSet解析
- Java集合框架之二:LinkedList源码解析
- Java数据结构-集合类
- 【Unity3D数据集合】(三)字典Dictionary和哈希表Hashtable学习
- Java集合框架2
- Java实现高效的枚举元素集合
- Java集合中的细节