Additive equations--zoj
来源:互联网 发布:淘宝店铺销量怎么计算 编辑:程序博客网 时间:2024/05/21 01:31
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
第一个数字表示输入数据的数量,之后每行第一个数表示有几个可供使用的数字,然后用这些数字组成加法等式,同一个数字不能重复使用,但是多次输入的数字可以重复使用。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[50],b[50],vis[1000010],flog,n;void bfs(int pos,int num,int sum,int key){/*pos指向a数组,选择a中的数,num表示b数组中数字个数,sum表示当前和key表示最多使用的数字个数*/if(num>key)return ;if(sum>a[n-1])/*最大的和a数组的最后一个数*/return ;if(num==key&&vis[sum]){flog=0;/*当找到一种符合情况的时,flog赋值*/int i;for(i=0;i<key-1;i++)printf("%d+",b[i]);printf("%d=%d\n",b[key-1],sum);return ;}if(pos>=n)return ;b[num]=a[pos];bfs(pos+1,num+1,sum+a[pos],key);/*对于当前操作的数,有两种选择,要或不要,要的话num+1,否则不加,之后这个数会被下一个数覆盖*/bfs(pos+1,num,sum,key);}int main(){int t;scanf("%d",&t);while(t--){int i;scanf("%d",&n);memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(vis,0,sizeof(vis));for(i=0;i<n;i++){scanf("%d",&a[i]);vis[a[i]]=1;/*vis数组标记*/}flog=1;sort(a,a+n);for(i=2;i<n;i++)bfs(0,0,0,i);if(flog)printf("Can't find any equations.\n");printf("\n");}return 0;}
- ZOJ 1204 Additive equations
- ZOJ 1204Additive equations
- ZOJ 1204.Additive equations
- zoj - 1204 - Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204 Additive equations
- zoj Additive equations
- ZOJ 1204 Additive equations
- zoj 1204 Additive equations
- zoj 1204 Additive equations
- zoj 1204 Additive equations
- ZOJ 1204Additive equations
- ZOJ 1204 Additive equations
- Additive equations--zoj
- ZOJ 1204 Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204(Additive equations)
- ZOJ - 1204 Additive equations(搜索)
- NSArray的使用方法
- 【文档】事件(Event)知识整理
- 推荐系统简介
- UVA 297 Tree
- 深入JavaScript(6)S.O.L.I.D五大原则之单一职责SRP
- Additive equations--zoj
- java常见错误及解决方法
- 关键字volatile
- 关于开发人员申请苹果账号相关事宜
- Python爬虫3
- android-ndk开发helloworld
- 深入JavaScript(7)S.O.L.I.D五大原则之开闭原则OCP
- 用OC语言完成简易通讯录(实现增删改查)功能.
- HDU 1394 Minimum Inversion Number