HDU 1258&&POJ 1564&&ZOJ 1711 DFS
2014-02-09 19:55
316 查看
题意:给你一个目标值 t ,n个正整数;问有多少种不同的和等于 t。。。
#include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <stack> #include<algorithm> #include<set> using namespace std; #define INF 1e8 #define eps 1e-8 #define LL long long #define maxn 2 #define mol 1000000007 int t,n,a[15],num[15]; int j,flag; void dfs(int x,int sum) { if(sum>t) return; else if(sum==t) { flag=1; printf("%d",num[0]); for(int i=1;i<j;i++) printf("+%d",num[i]); printf("\n"); return; } else { int tem=-1; for(int i=x+1;i<=n;i++) { if(a[i]!=tem) { tem=a[i]; num[j++]=a[i]; dfs(i,sum+a[i]); j--; } } } } int main() { while(scanf("%d%d",&t,&n)) { if(!t) break; for(int i=1;i<=n;i++) scanf("%d",&a[i]); memset(num,0,sizeof(num)); j=0;flag=0; printf("Sums of %d:\n",t); dfs(0,0); if(!flag) printf("NONE\n"); } return 0; }
相关文章推荐
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
- ZOJ 1978 && HDU 1216 && POJ 2552 Assistance Required (筛选)
- POJ 2777 && ZOJ 1610 &&HDU 1698 --线段树--区间更新
- POJ 3653 & ZOJ 2935 & HDU 2722 Here We Go(relians) Again(最短路dijstra)
- POJ 3653 & ZOJ 2935 & HDU 2722 Here We Go(relians) Again(最短路dijstra)
- hdu 4071& poj 3873 & zoj 3386 & uva 12197 Trick or Treat 三分法
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
- ZOJ 1103(POJ 2415)(HDU 1252)Hike…
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
- poj 1543 & HDU 1334 & ZOJ 1331 Perfect Cubes(数学 暴力大法好)
- POJ 1564 && HDU 1258 Sum It Up(dfs)
- zoj 1149 && hdu 1059 && poj 1014 Dividing
- hdu 1342&&poj 2245&&zoj 1089 Lotto
- POJ 3654 & ZOJ 2936 & HDU 2723 Electronic Document Security(模拟)
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
- poj 1564 && zoj 1711 Sum It Up (dfs)
- POJ 3654 & ZOJ 2936 & HDU 2723 Electronic Document Security(模拟)
- 8月10号的练习:ZOJ 3203&&POJ 3974&&HDU 1394&&HDU 3400&&HDU 2152
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)