hdu-1258-Sum It Up(DFS)
来源:互联网 发布:前端ajax获取json数据 编辑:程序博客网 时间:2024/05/16 18:07
题目链接
题意:给定一个非递减的序列,要求从这些序列中找出一系列的数相加等于要求的数
思路:简单深搜
code:
#include<cstdio>#include<algorithm>using namespace std;int a[20],b[20],sum;int n,len,cut;bool cmp(int a,int b){ return a>b;}void dfs(int x,int y,int sum,int m){ if(sum>n||m>=len)return ; if(sum==n){ cut++; printf("%d",b[0]); for(int i=1;i<m;i++){ printf("+%d",b[i]); } printf("\n"); return ; } for(int i=y;i<len;i++){ b[m]=a[i]; dfs(a[i],i+1,sum+a[i],m+1); while(i+1<len && a[i] == a[i+1]) i++; }}int main(){ while(~scanf("%d %d",&n,&len)&&len){ for(int i=0;i<len;i++) scanf("%d",&a[i]); sort(a,a+len,cmp); printf("Sums of %d:\n",n); cut=0; dfs(0,0,0,0); if(!cut)printf("NONE\n"); } return 0;}
阅读全文
2 0
- hdu 1258 Sum It Up(DFS)
- hdu 1258 Sum It Up(dfs)
- HDU 1258 Sum It Up (DFS)
- HDU 1258 Sum It Up(DFS)
- HDU 1258 Sum It Up(DFS)
- hdu-1258-Sum It Up(DFS)
- hdu 1258 Sum It Up(dfs)
- hdu 1258 Sum It Up dfs
- HDU 1258 Sum It Up DFS
- HDU 1258 Sum it up 搜索DFS
- HDU--1258:Sum It Up (DFS)
- hdu 1258 Sum It Up (dfs)
- hdu 1258 Sum It Up(dfs)
- HDU 1258 Sum It Up (还是DFS)
- hdu 1258 Sum it up dfs+剪枝
- HDOJ(HDU).1258 Sum It Up (DFS)
- hdu 1258 Sum It Up dfs暴搜
- HDU 1258 (DFS) Sum It Up
- angularjs2 组件的生命周期
- 自然数立方的乐趣
- TCP与UPD知识总结
- 51Nod 1242:斐波那契数列的第N项
- 【牛客网】直通bat-面试算法精品课_第2章 排序 2.5 归并排序练习题(JAVA版)
- hdu-1258-Sum It Up(DFS)
- 区别#define、enum和const
- A Dangerous Maze LightOJ
- Polycarp and Letters
- hdu2853 Assignment
- poj 3984 bfs+路径还原
- 安卓 手写消息机制
- GET和POST有什么区别?
- 十字链表和邻接多重表