ZOJ 1204.Additive equations
来源:互联网 发布:apache storm 编辑:程序博客网 时间:2024/05/10 14:09
给出一个整数集合,判断是否能用集合里的元素构造加法等式,若能则输出按顺序输出所有等式。
简单DFS()应用。
/*ZOJ 1204 Additive equationsSample Input33 1 2 33 1 2 56 1 2 3 5 4 6Output for the Sample Input1+2=3Can't find any equations.1+2=31+3=41+4=51+5=62+3=52+4=61+2+3=6*/#include<iostream>#include<algorithm>using namespace std;bool flag; //判断是否存在等式int *A; //存储集合bool *visited; //标识是否计算void Create(int n){ //创建集合 for(int i=0; i<n; i++){ cin>>A[i]; } sort(A,A+n);}void DFS(int start,int len,int n,int result){ //寻找式子左边长度为len的等式 if(len == 0){ for(int i=start; i<n && A[i]<=result; i++){ if(result == A[i]){ flag = true; for(int j=0; j<n && result != 0; j++){ if(visited[j]){ if(result == A[j]){ cout<<A[j]<<'='<<A[i]<<endl; } else{ cout<<A[j]<<'+'; } result -= A[j]; } } } } } else { if(len > 0){ for(int i=start; i<n; i++){ if(result + A[i] <= A[n-1]){ result += A[i]; len--; visited[i] = true; DFS(i+1,len,n,result); visited[i] = false; len++; result -= A[i]; } } } }}void Search(int A[],int n,bool visited[]){ //寻找等式 for(int i=2; i<n; i++){ DFS(0,i,n,0); }}int main(){ int t; //用例数量 int n; //集合元素 cin>>t; while(t--){ cin>>n; A = new int[n]; visited = new bool[n]; Create(n); flag = false; Search(A,n,visited); if(!flag){ cout<<"Can't find any equations."<<endl; } cout<<endl; } return 0;}
- ZOJ 1204.Additive equations
- ZOJ 1204 Additive equations
- ZOJ 1204Additive 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(搜索)
- Velocity学习笔记1
- io操作
- Velocity学习笔记2
- 《编程之美》蚂蚁爬杆问题的扩展【1】
- 查询Flashback_transaction_query 报错ORA-01031处理
- ZOJ 1204.Additive equations
- Velocity学习笔记3
- iOS 中如何添加应用自己的字体
- jQuery调用WebService返回JSON数据
- Android中SharedPreferences(共享参数)用法
- SurfaceView的学习01
- 编程真难。。。
- IOS获取屏幕分辨率
- Windows7帮你监控记录电脑使用