zoj 1204
来源:互联网 发布:c语言编程软件 编辑:程序博客网 时间:2024/06/05 11:46
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.
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
Source: Zhejiang University Local Contest 2002, Preliminary
一道关于DFS的题目,很早就学过深度优先搜索了,但是一直没有运用的实际中,所以虽然知道这是一道可以用DFS来解决的题,但是依旧不知道怎么写。
不过最后还是AC了,效率有点低
#include<iostream>#include<algorithm>using namespace std;int n,m;int num[31];bool flag;bool hash[31] = {0};void DFS(int nflag, int sum, int curr, int depth){if(curr == depth){for(int i=nflag; num[i]<=sum; i++){if(sum == num[i]){flag = 1;int tempsum = sum;for(int j=0; j<m; j++){if(hash[j]){if(tempsum == num[j])cout<<num[j]<<"="<<num[i]<<endl;else{tempsum -= num[j];cout<<num[j]<<"+";}}}}}}else if(curr < depth){for(int i=nflag; i<m; i++){if(sum+num[i] <= num[m-1]){sum += num[i];curr++;hash[i] = 1;DFS(i+1, sum, curr, depth);sum -= num[i];hash[i] = 0;curr--;}}}}int main(){int i;cin>>n;while(n--){cin>>m;for(i = 0; i < m; i++)cin>>num[i];sort(num, num+m);flag = 0;for(i = 2; i < m; i++)DFS(0,0,0,i);if(!flag)cout<<"Can't find any equations."<<endl;cout<<endl;}return 0;}
- zoj 1204
- ZOJ-1204
- ZOJ 1204
- zoj 1204
- ZOJ 1204 Additive equations
- zoj 1204 dfs
- ZOJ 1204Additive equations
- zoj - 1204 - Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204 Additive equations
- zoj 1204 Additive equations
- zoj 1204 Additive equations
- ZOJ ACM 1204 (JAVA)
- zoj 1204 Additive equations
- ZOJ 1204Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204 Additive equations
- 用例图总结
- 两种方法接口aspx页面的编写(页面展示xml文档)
- c++ cout 格式化输出示例
- VS2010\2008中不能新建项目问题的解决
- RUIM卡
- zoj 1204
- Android 2.3 如何去除camera的gps定位
- Oracle Record is Locked by anther user 表被锁
- Ubuntu 系统中汉字不能显示的修正方法[持续添加中]
- 初装Linux网络优化
- Android全屏绘制 Surface的使用
- 要养成良好的书写Sql的习惯 ---------转自wanghangzhou1984
- 从甲乙丙的关系看自然语言理解的“第二路径”
- php 今天星期几