poj1564-Sum It Up(经典DFS)

给出一个n,k,再给出的n个数中,输出所有的可能使几个数的和等于k

Sample Input

4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
Sample Output

Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25

明显的DFS,这个dfs方程让我纠结啊,递归的我头都大了,但是看下答案稍微来点灵感了,在这里dfs函数方程要有哪些参数?

首先要从当前数往后开始dfs,所以要有个参数是当前搜索的数组下标

其次,要判断和=t,所以还要有个保存当前的和的参数,在这里我用t减去当前和,所以当此参数等于0那么就是找到满足条件

最后要输出此序列,所以还要有个参数来标记当前要找的数在数组里的位置

找到三个参数后就好办多了,看代码吧


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

#include <stdio.h>

#include <string.h>

#include <algorithm>

using namespace std;

int n,t;

int num[101];

int r[101];

bool flag;

void dfs(int len,int k,int last)

{

    int result;

    if(last==0)

    {

        flag=false;

        for(int i=0;i<len;i++)

        if(i==0)printf("%d",r[i]);

        else printf("+%d",r[i]);

        printf("\n");

        return ;

    }

    for(int i=k;i<n;i++)

     {

         

         if(i==k||num[i]!=num[i-1]&&last-num[i]>=0)// 去除重复的操作

         {

         r[len]=num[i];

         dfs(len+1,i+1,last-num[i]);

         }      

     }

}

int main()

{

    while(scanf("%d%d",&t,&n)!=EOF)

    {

        if(t==n&&n==0)break;

        flag=true;

        for(int i=0;i<n;i++)

        scanf("%d",&num[i]);

        printf("Sums of %d:\n", t);

        dfs(0,0,t);

        if(flag)printf("NONE\n");

    }

    

}

时间: 2024-10-06 20:11:06

poj1564-Sum It Up(经典DFS)的相关文章

EOJ1981 || POJ1011 经典dfs+剪枝+奇怪的数据

题目:EOJ1981 || POJ1011   经典dfs+剪枝+奇怪的数据 Description George took sticks of the same length and cut them randomly until all partsbecame at most 50 units long. Now he wants to return sticks to the originalstate, but he forgot how many sticks he had origi

poj1564 Sum it up

题目链接: http://poj.org/problem?id=1564 题目: Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5839   Accepted: 2984 Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list t

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

HDU1258 Sum It Up 【DFS】+【判重】

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3944    Accepted Submission(s): 2026 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

HDU 1258 Sum It Up (dfs)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a[20]; int ans[20]; int num[200]; struct Mark { int shu,rt; }; Mark mark[20]; int ok; int n,t; int cnt; int cmp(int x,int y) { retur

hdu 1258 Sum It Up(dfs+去重)

题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = [4,3,2,2,1,1]. 有四种不同的方法使得它们相加的结果等于total(即等于4),分别为:4,3+1,2+2, 2+1+1. 在同一种拼凑方式中,每个数字不能被重复使用,但是在list中可能存在许多相等的数字. 输入: 输入包含许多测试用例,每个用例仅占一行.每个用例包含t(total)

POJ 1564 经典dfs

1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define F(i,a,b) for (int i=a;i<=b;i++) #define mes(a,b) memse

POJ 1564 Sum It Up(DFS)

Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if

经典DFS问题实践

八皇后问题: 1 //八皇后问题 经典的DFS问题实践 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstdio> 6 using namespace std; 7 int mat[8][8]; 8 int ans=0; 9 bool check(int row,int col)//最终检查满足要求,则return 1 10 { 11 for(int i=0;