poj1564

dfs判重

#include<stdio.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<queue>
#include<stack>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<algorithm>
using namespace std;
#define ll unsinged long long
#define PI acos(-1.0)
#define ING 0x7fffffff
#define INF 0x3f3f3f3f
#define mod 1000000007
#define mod 5201314
#define N 1005
int n,m;
int a[N];
int d[N];
int flag;
void dfs(int x,int sum,int num)
{
    if(sum>n)
        return;
    if(sum==n)
    {
        flag=1;
        printf("%d",d[0]);
        for(int i=1;i<num;i++)
        {
            printf("+%d",d[i]);
        }
        printf("\n");
        return;
    }
    for(int i=x;i<m;i++)
    {
        d[num]=a[i];
        dfs(i+1,sum+a[i],num+1);
        while(a[i+1]==a[i])//判重
            i+=1;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        for(int i=0;i<m;i++)
        {
            scanf("%d",&a[i]);
        }
        printf("Sums of %d:\n",n);
        flag=0;
        dfs(0,0,0);
        if(flag==0)
            printf("NONE\n");
    }
    return 0;
}
posted @ 2016-04-21 16:48  see_you_later  阅读(153)  评论(0编辑  收藏  举报