先上原题

Additive equations
Time Limit: 10000 msMemory Limit: 32768 KB
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

思路

1、首先,我要解释下我是怎么找出这道题的解法是深度搜索的。

假设我输入了一组数据:4 1 2 3 4(意味着有4个数,分别为1 2 3 4)
那么我要怎么找出所有的 Addtive Equations 呢?
我是通过一棵树来寻找的:


在这张图中,深度越深,代表找到的这个 Addtive Equations 等号左边的数越多。比如第四层就是代表1+2+3+4 。
我们要按照这张图的方法去遍历输入的每一组数据,显然,要用到的方法是深搜。

代码

(代码注释得很清晰,我就不多解释了)

#include <iostream>
#include <algorithm>
using namespace std;int set[100];//保存输入的每一组数据
bool visit[100];//用于记录有无访问过的点,用于输出 Addtive Equations 等号左边的数
bool has;//判断当前组数据有无 Addtive Equation//初始化set和visit
void init() {for (int i = 0; i < 100; ++i) {set[i] = 0;visit[i] = 0;}
}void display(int setnum,int sum) {int i = 0;//找到第一个访问过的数while ( !visit[i] && i<setnum ) ++i;cout << set[i];i = i + 1;//找到后续访问过的数while (i < setnum) {if (visit[i]) {cout << "+" << set[i];}++i;}cout << "=" << sum<<endl;
}void DFS(int sum,int num,int pos,int depth) {//sum保存以访问点的和,num代表这组数据中有多少个数,pos指出当前访问点在set中的下标位置,depth记录着与目标深度的距离//如果当前访问的数就是一组数据最后的一个数,那么后面肯定是没有数等于它和前面访问过的数的和if (pos >= num) return;//找到一个Addtive Equation,同时是符合当前需要寻找的深度if (!depth) {for (int i = pos; i < num; ++i) {if (set[i] == sum && visit[i] == 0) {//输出这一个 Addtive Equationdisplay(num, sum);has = 1;break;}}}for (int k = pos; k < num; ++k) {//剪枝,如果当前访问点和之前访问点的和大于这一组数据的最后一个数(最后一个数也是最大一个数),//那么之前访问点和后面将要访问的点的和肯定也大于最后一个数//不剪枝AC不了,会超时if (sum + set[k] > set[num - 1]) break;//对当前访问点进行深度搜索visit[k] = 1;DFS(sum + set[k], num, k + 1,depth-1);//不走当前访问点visit[k] = 0;}
}int main()
{int setNum; int lengths;//lengths是搜索的深度,代表着Addtive Equations等号左边的整数数量cin >> setNum;while (setNum) {int num;init();lengths = 2;//Addtive Equations等号左边最少也要两个数has = 0;cin >> num;for (int i = 0; i < num; ++i) {cin >> set[i];}//对左边整数集进行排序sort(set, set + num);while (lengths <= num) {DFS(0, num, 0, lengths);++lengths;}if (!has) cout << "Can't find any equations." << endl;cout << endl;setNum = setNum - 1;}system("pause");return 0;
}

踩坑

第一次我没有进行剪枝,在oj平台上怎么也AC不了(显示超时)。以后深搜如果超时了,要进行剪枝。

看了后觉得有收获,记得点赞

ZOJ 1204 Additive equations相关推荐

  1. zoj 1204 Additive equations

    ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...

  2. Additive equations

    题目描述 We all understand that an integer set is a collection of distinct integers. Now the question is ...

  3. 4、Macbook2015 A1502 笔记本的换屏过程

    屏幕出现竖线,显示屏出现问题 笔记本型号A1502,淘宝购入外屏, 如果动手能力MAX的同学可以更换排线, 排线位于屏幕下方的黑色条中,这个需要撬开,不太好拆, 排线位于左侧拆下更换即可, 成本50块 ...

  4. POJ ZOJ题目分类

    POJ,ZOJ题目分类(多篇整合版,分类很细致,全面) 标签: 题目分类POJ整理 2015-04-18 14:44 1672人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: ACM资料(5) ...

  5. POJ,ZOJ题目分类(多篇整合版,分类很细致,全面)

    水题: 3299,2159,2739,1083,2262,1503,3006,2255,3094 初级: 一.基本算法:        (1)枚举 (1753,2965)       (2)贪心(13 ...

  6. ZOJ 题目分类,学校的一个巨巨做的。

     DP: 1011      NTA                    简单题 1013      Great Equipment        简单题 1024      Calendar ...

  7. c语言大小写字母互换1005,1005 Jugs,1005jugs

    1005 Jugs,1005jugs 辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. 1 #include 2 3 intmain(){4 intA,B,T,sA,sB;5 while(sca ...

  8. poj题目详细分类及算法推荐题目

    DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题  ...

  9. ACM POJ 题目分类(完整整理版本)

    DP: 1011   NTA                 简单题  1013   Great Equipment     简单题  1024   Calendar Game       简单题   ...

最新文章

  1. LINQ via C# 系列文章
  2. jar包反编译成源代码_IntelliJ IDEA 如何设置自动下载源代码和文档
  3. 二十万字C/C++、嵌入式软开面试题全集宝典九
  4. bugku ——杂项
  5. 前端学习(1168):构造函数方法(转换为真正数组)
  6. ❤️ 给你的Linux把把脉(内存、磁盘、CPU、网络)❤️
  7. postgresql 并发访问_PostgreSQL并发控制(显式锁定)
  8. PAT (Basic Level) Practise:1013. 数素数
  9. 蓝桥杯 PREV-32 历届试题 分糖果
  10. 分布式搜索引擎Elasticsearch的简单使用
  11. linux mongo 添加用户名和密码,mongodb3.4 安装及用户名密码设置
  12. 数据挖掘RapidMiner工具使用----聚类K-Means案例分析
  13. 项目管理表格模板/实用表格-项目启动
  14. h5算命php源码,H5付费算命PHP源码那么火_付费算命源码如何下载
  15. yii ccaptcha php模块,yii验证码无法显示
  16. js实现视频直播,结合bilibili开源项目
  17. Python 之CV2详解
  18. 工具(五)--将doc格式文件批量转为docx
  19. Echarts地图 绘制自定义区域 - geojson.io使用方法
  20. Jquery全选系列操作(锋利的jQuery)

热门文章

  1. HTML 编辑器的介绍及推荐
  2. matlab stereo,matlab stereo_gui立体标定
  3. 中南大学2008级计算机科学张磊,2008级本科生学分制收费标准
  4. 网管软件Zabbix3.4.8 使用笔记 - 安装配置 20180509
  5. 【推荐】《Android应用安全设计及安全编码指导手册》更新到2016年9月1日版本
  6. 园区写字楼都在用的物业管理系统
  7. 《Linux进程控制---编写简易shell实现》
  8. linux抓包dcp,Wireshark DCP-ETSI 解析器拒绝服务漏洞
  9. glog 打印级别设置_久其系统出现打印ocx未正确安装的解决办法
  10. 最小生成树算法及例题