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) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=10010,MAX=1000100;

bool flag;
int t,n;
int num[20],ans[20];

void dfs(int cnt,int k,int sum)
{
    if(sum==t){
        flag=false;
        F(i,1,cnt-1){
            if(i==1)printf("%d",ans[i]);
            else printf("+%d",ans[i]);
        }
        printf("\n");
        return ;
    }

    //关键
    F(i,k,n){
        if(i==k||num[i]!=num[i-1]&&sum+num[i]<=t){
            ans[cnt]=num[i];
            dfs(cnt+1,i+1,sum+num[i]);
        }
    }
}

int main()
{
    while(~scanf("%d%d",&t,&n),t&&n)
    {
        flag=true;
        F(i,1,n)scanf("%d",&num[i]);
        printf("Sums of %d:\n",t);
        dfs(1,1,0);
        if(flag)printf("NONE\n");

    }

    return 0;
}

时间: 2024-10-10 18:01:14

POJ 1564 经典dfs的相关文章

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 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 t = 4

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

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

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存储放置的数 /

[POJ 1011]Sticks(DFS剪枝)

Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original

poj 1564 Sum It Up (DFS+ 去重+排序)

http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i]和上一个数相同,那么记录数组的同一个位置就没有必要再放入这个数.例如:4 3 3 2构成和是7,b数组的第二个位置放了3,则后面的那个3就没有必要再放入记录数组的第二个位置了.(可能会放到后面的位置)... #include<stdio.h> #include<string.h> #i

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

POJ 1011 Sticks(经典dfs)

Language: Default简体中文 Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 120720   Accepted: 27951 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to r

poj 1564 Sum It Up【dfs+去重】

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Accepted: 3475 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 t = 4, n