POJ 1564 Sum It Up
来源:互联网 发布:美剧 英文 知乎 编辑:程序博客网 时间:2024/04/30 16:12
题目大意:给你N个数,加起来等于T的方式有多少种,不重复的输出出来。
思路:先用深搜搜出可选结果。然后再结果中找到那些已经选中的结果。具体筛选过程看注释
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int tot,n;int a[1005];int vis[1005];int v[1005];int t[1005];int k;int flag;int dfs(int sum,int cur,int pos){ // printf("sum=%d cur=%d\n",sum,cur); if(sum==tot) { flag=1; for(k=0;k<cur-1;k++) printf("%d+",t[k]); printf("%d",t[k]); printf("\n"); return 1; } else if(sum>tot)return 0; for(int i=pos;i<n;i++) { if(vis[i]==1)continue; if(sum+a[i]<=tot) { vis[i]=1; t[cur]=a[i]; dfs(sum+a[i],cur+1,i); vis[i]=0; } while(i<n&&a[i]==a[i+1]) //此处为筛选,就是在搜索完成以后,那么这个数字的所有可能情况都被搜过了,就可以直接跳过了。 i++; }}int main(){ while(scanf("%d%d",&tot,&n)!=EOF && tot && n) { flag=0; memset(vis,0,sizeof(vis)); memset(v,0,sizeof(v)); for(int i=0;i<n;i++) scanf("%d",&a[i]); printf("Sums of %d:\n",tot); dfs(0,0,0); if(!flag)printf("NONE\n"); } return 0;}
- POJ 1564 - Sum It Up
- poj 1564 Sum It Up
- poj 1564 Sum It Up
- POJ 1564 Sum It Up
- POJ-1564-Sum It Up
- poj 1564 Sum It Up
- POJ 1564 Sum It Up
- POJ 1564 Sum It Up
- POJ 1564 Sum It Up
- poj 1564 Sum It Up
- Poj 1564 Sum It Up
- poj 1564 Sum It Up
- Poj Sum It Up
- poj sum it up
- poj sum it up
- POJ 1564 Sum It Up dfs
- poj 1564 Sum It Up -- DFS 递归
- POJ 1564 Sum It Up(DFS)
- WM_COMMAND 和 WM_NOTIFY 区别
- java平台的脚本语言可以利用起来了 4
- 列表标题栏添加CheckBox
- Nginx、SSL双向认证、PHP、SOAP、Webservice、https
- 俄罗斯方块(C语言实现)
- POJ 1564 Sum It Up
- Java中:包、类、字段、方法命名规则
- 浅谈Java Applet应用程序的编辑与运行
- 关于Oracle TNS_ADMIN环境变量
- AudioTrack与AudioFlinger交换音频数据
- 【θ如何屏蔽网页广告θIE10屏蔽网页中的广告教程θ】
- ACE_Message_Block功能简介
- 【你所不知道的惊人减肥食品】
- 高中的地理老师