DFS Additive equations (累加)
来源:互联网 发布:数据建模分析师 编辑:程序博客网 时间:2024/06/06 03:19
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.
33 1 2 33 1 2 56 1 2 3 5 4 6
Output for the Sample Input
1+2=3Can't find any equations.1+2=31+3=41+4=51+5=62+3=52+4=61+2+3=6
题意:
给你一个集合,让你写出集合中满足的数学表达式,如果一个也没有输出 Can't find any equations.
思路:
从数学表达式的长度入手,从2到n-1,DFS#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int num[35],vis[35],n,flag;void DFS(int start,int len,int sum){ if(len==0) { //当长度为0说明表达式等号前的已经搜索完成 for(int i=start;i<=n&&sum>=num[i];i++) { //从start开始 是有可能成为表达式等号后的数值,且等号前的和值要大于num[i] if(sum==num[i]) { flag=1; //验证是否存在表达式 for(int j=1;j<=i;j++) { //表达式等号前的数字 if(vis[j]) { //是否使用num[j] if(sum==num[j]) printf("%d=%d\n",num[j],num[i]); else printf("%d+",num[j]); sum=sum-num[j]; } } } } } else { for(int i=start;i<=n;i++) { if(sum+num[i]<=num[n]) { //表达式等号前的数值要小于集合种的最后一个数 sum=sum+num[i]; //计算和值 vis[i]=1; //标记使用 --len; //长度减一 DFS(i+1,len,sum); vis[i]=0; len++; sum=sum-num[i]; } } }}int main(){ int icase; scanf("%d",&icase); while(icase--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&num[i]); memset(vis,0,sizeof(vis)); sort(num+1,num+n+1); flag=0; for(int i=2;i<n;i++) DFS(1,i,0); if(!flag) printf("Can't find any equations.\n"); printf("\n"); } return 0;}
- DFS Additive equations (累加)
- ZOJ 题目1024 Additive equations(DFS)
- (搜索DFS,剪枝)Additive equations
- ZOJ-1204 Additive equations (DFS)
- (zoj1204)Additive equations
- ZOJ 1204--Additive equations【DFS && 好题】
- Additive equations
- Additive equations
- Additive equations
- Additive equations
- Additive equations
- zoj 1204 Additive equations(DFS,我快哭了。。。)
- ZOJ1204 Additive equations,DFS+set+仿函数+剪枝
- ZOJ 1204 Additive equations(深搜)
- ZOJ 1204 Additive equations
- ZOJ 1204Additive equations
- ZOJ 1204.Additive equations
- zoj - 1204 - Additive equations
- 淘宝轮播图效果
- java中使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?
- 在curl中设置自定义的HTTP头
- Light OJ 1005 Rooks
- 浅谈CMS垃圾收集器与G1收集器
- DFS Additive equations (累加)
- 数据库事务隔离级别-- 脏读、幻读、不可重复读
- 用js写的一个红绿灯程序
- @JSONField作用
- mysql 行级锁的使用以及死锁的预防
- C语言基础知识点(二)三大控制结构
- 叶子结点带权路径长度和(先序遍历法和层序遍历法)
- 好听的英文网名
- 语法,句群,语篇