hdu1258
来源:互联网 发布:无数据库论坛源码 编辑:程序博客网 时间:2024/05/29 19:22
4 6 4 3 2 2 1 15 3 2 1 1400 12 50 50 50 50 50 50 25 25 25 25 25 250 0
Sums of 4:43+12+22+1+1Sums of 5:NONESums of 400:50+50+50+50+50+50+25+25+25+2550+50+50+50+50+25+25+25+25+25+25
一个简单的递归搜素,但是关键点在于组合问题,也就是用for(i = 0;i < n;i++)这种从0开始的搜索会出错。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
int t,n;
int array[13];
int temp[13];
bool visited[13];
int flag;
int cmp(int a,int b)
{
if(a > b)
return 1;
else
return 0;
}
void DFS(int count,int k,int x)
{
if(count == t)
{
flag = 1;
for(int i = 0;i < k - 1;i++)
{
cout<<temp[i]<<"+";
}
cout<<temp[k-1]<<endl;
return;
}
for(int j = x;j < n;j++)
{
if(count + array[j] <= t)
{
temp[k] = array[j];
DFS(count + array[j],k+1,j+1);
while(j + 1 < n&&array[j+1] == array[j])
j++;
}
}
}
int main()
{
int i,j,k;
while(~scanf("%d%d",&t,&n))
{
flag = 0;
if(n == 0)
break;
memset(visited,false,sizeof(visited));
for(i = 0;i < n;i++)
{
scanf("%d",&array[i]);
}
sort(array,array+n,cmp);
cout<<"Sums of "<<t<<":"<<endl;
DFS(0,0,0);
if(!flag)
cout<<"NONE"<<endl;
}
return 0;
}
- hdu1258
- hdu1258
- HDU1258
- HDU1258
- hdu1258
- hdu1258(dfs)
- hdu1258/poj1564 dfs
- hdu1258 DFS+回溯
- HDU1258 Sum It Up
- HDU1258:Sum It Up
- hdu1258(DFS)
- Sum It Up hdu1258
- HDU1258 Sum it up
- HDU1258 Sum It Up
- hdu1258 dfs深搜
- HDU1258:Sum It Up(DFS)
- hdu1258 Sum It Up (DFS)
- Sum It Up(hdu1258,dfs)
- Servlet监听器和过滤器基本使用
- session对象
- 用python获取cmd下的相关信息
- Java在读取文件内容的时候,如何判断出空白行
- 树莓派学习资料链接
- hdu1258
- ZOJ3946-Highway Project
- 相对定位和绝对定位以及固定定位
- C/C++常用头文件及函数汇总
- 编解码基础2
- 报头压缩-Linux下安装ROHC协议库
- 堆栈
- JSTL标签和常用函数的使用
- 数据访问层(连接数据库)