ZOJ1204——Additive equations(DFS)

Additive equations

Description
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

题目大意:

    给定一个数列 找出其中的加法等式x1+x2+x3+..xn=y(其中x1,x2,x3,xn,y属于数列) (n>=2)

结题思路:

    可以将数列看做一个无向完全图(即每个顶点都指向其他所有顶点)。用DFS搜索,将符合题目要求的存起来,再排序输出即可。 具体细节请看代码(语死早,没办法^^)

    细节:

      1)符合要求的等式比能保证x1<x2<x3<<xn<y

      2)搜索前排好序,由小到大。根据1)可知只搜素比当前元素靠后的元素是否等于当前递归的总和即可。

Code:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #define MAXN 30000
 7 using namespace std;
 8 int a[50],flag[50];
 9 int N;
10 struct s //用于存等式用的结构体,记录等式中的各个元素和元素个数(最后一个元素必为等号右边元素)
11 {
12     int a[50];
13     int lenth;
14 } str[MAXN];
15 int k=0;
16 void output() //输出函数,k表示搜索后的符合要求的等式的数量。
17 {
18     if (k==0) printf("Can‘t find any equations.\n\n");
19     else
20     {
21         for (int i=1; i<=k; i++)
22         {
23             int j;
24             printf("%d",str[i].a[1]);
25             for (j=2; j<str[i].lenth; j++)
26                 printf("+%d",str[i].a[j]);
27             printf("=%d\n",str[i].a[j]);
28         }
29         printf("\n");
30     }
31 }
32 void input()//DFS到符合要求的等式,将等式的各个元素存入数组
33 {
34     k++;
35     str[k].lenth=0;
36     int t=1;
37     for (int i=1; i<=N; i++)
38         if (flag[i]!=0) str[k].a[t++]=a[i],str[k].lenth++;
39 }
40 void DFS(int i,int sum) //flag[i]==1表示当前DFS中 i被使用了。
41 {                       //当递归到符合条件的时候可根据当前的flag数组情况来获取等式的相关元素
42     sum+=a[i];
43     if (sum>a[N]) return ;
44     for (int j=i+1; j<=N; j++)
45         if (sum==a[j])
46         {
47             flag[j]=1;
48             input();
49             flag[j]=0;
50         }
51     for (int j=i+1; j<=N; j++)
52     {
53         flag[j]=1;
54         DFS(j,sum);
55         flag[j]=0;
56     }
57 }
58 bool cmp(struct s a,struct s b)//根据题目要求排序,保证短的在前,相同长度情况下数字小的在前
59 {
60     if (a.lenth!=b.lenth) return a.lenth<b.lenth;
61     for (int i=1; i<=a.lenth; i++)
62         if (a.a[i]!=b.a[i]) return a.a[i]<b.a[i];
63 }
64 int main()
65 {
66     int T;
67     cin>>T;
68     while (T--)
69     {
70         memset(flag,0,sizeof(flag));
71         memset(a,0,sizeof(a));
72         k=0;
73         cin>>N;
74         for (int i=1; i<=N; i++)
75             cin>>a[i];
76         sort(a+1,a+1+N);
77         for (int i=1; i<=N; i++)
78         {
79             flag[i]=1;
80             DFS(i,0);
81             flag[i]=0;
82         }
83         sort(str+1,str+k+1,cmp);
84         output();
85     }
86     return 0;
87 }

ZOJ1204——Additive equations(DFS)

时间: 2024-10-09 21:37:26

ZOJ1204——Additive equations(DFS)的相关文章

ZOJ 题目1024 Additive equations(DFS)

Additive equations Time Limit: 10 Seconds      Memory 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 ad

DFS——Additive equations

Additive equations Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 15   Accepted Submission(s) : 9 Problem Description We all understand that an integer set is a collection of distinct integers

Additive equations

题目描述 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 i

ZOJ 1204 Additive equations(深搜)

Additive equations Time Limit: 10 Seconds      Memory 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 ad

ZOJ 1204--Additive equations【DFS &amp;&amp; 好题】

Additive equations Time Limit: 10 Seconds      Memory 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 ad

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

How Many Equations Can You Find(dfs)

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 714    Accepted Submission(s): 467 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5

UVA 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0