ZOJ 1204 Additive equations
来源:互联网 发布:suse linux dns配置 编辑:程序博客网 时间:2024/05/16 08:26
- 地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204
- 题意
- 按顺序输出相加等于in[]的所有组情况
- 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
- Cant 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
- 解题思路
- 简单的DFS。因为时间有10s,所以我用了一个蠢办法,控制answer-算式的长度,从而实现按题要求输出。
- 代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int comp(const void *a,const void *b)
- {
- return *(int*)a-*(int*)b;
- }
- #define max 505
- int in[max];
- int vis[max];
- int n,flag;
- int all;
- int find(int sum)
- {
- int x;
- for(x=0;x<n;x++)
- if(in[x]==sum)
- return 1;
- return 0;
- }
- void dfs(int x,int sum,int len)
- {
- int i,a;
- if(find(sum)&&len==all)
- {
- int ans[max];
- int j=0;
- for(a=0;a<max;a++)
- {
- if(vis[a]!=0)
- ans[j++]=a;
- }
- if(j>1)
- {
- for(a=0;a<j-1;a++)
- printf("%d+",in[ans[a]]);
- printf("%d=%d\n",in[ans[a]],sum);
- flag=1;
- }
- }
- for(i=x;i<n;i++)
- {
- if(vis[i]==0&&(sum+in[i])<=in[n-1])
- {
- vis[i]=1;
- dfs(i+1,sum+in[i],len+1);
- vis[i]=0;
- }
- }
- }
- int main()
- {
- int t,i;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d",&n);
- for(i=0;i<n;i++)
- scanf("%d",&in[i]);
- qsort(in,n,sizeof(in[0]),comp);
- flag=0;
- for(all=1;all<=n;all++)
- {
- memset(vis,0,sizeof(vis));
- dfs(0,0,0);
- }
- if(flag==1)
- {
- printf("\n");
- }
- else
- printf("Can't find any equations.\n\n");
- }
- return 0;
- }
0 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 1204 Additive equations
- zoj 1204 Additive equations
- zoj 1204 Additive equations
- 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-1204 Additive equations (DFS)
- ZOJ 1204 Additive equations(深搜)
- machine learning(5) --AdaBoost分类器
- 2·14 情人&元宵节专题:半质数的个数-c#求解-英雄会在线编程题目
- 调用模板类出现 undefined reference 错误的解决方法
- 内核线程的实例与摧毁
- linux中怎样从底部向上查看log文件
- ZOJ 1204 Additive equations
- TOJ 2429 Find the Clones
- Remove Nth Node From End of List
- Server2008R2:由于没有远程桌面授权服务器可以提供许可证,远程会话被中断..错误的解决方法
- SQL语句优化--1
- 打印内存地址
- 分割字符串数组
- Eclipse的使用技巧之如何出现全部提示字符
- (standard c libraries translation )fileno