Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

没有看清楚题目输出顺序wa2次 dfs水过(附代码)参考过别人的..

Posted by Ink213 at 2013-04-04 16:38:46 on Problem 1564
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1005;
int x[MAXN], solv[MAXN];
int t, n;
bool flag;
bool cmp(int a,int b)
{
    return a>b;
}
void dfs(int nt,int deep,int p)
{
    int i;
    if(nt==t)
    {
        sort(solv+1,solv+deep,cmp);
        printf("%d",solv[1]);
        for(i=2; i<deep; i++)
        {
            printf("+%d",solv[i]);
        }
        puts("");
        flag=true;
        return ;
    }
    for(i=p+1; i<=n; i++)
    {
        solv[deep]=x[i];
        dfs(nt+x[i],deep+1,i);
        while(i<n&&x[i]==x[i+1]) i++;
    }
}
int main()
{
    int i, j;
    while(scanf("%d%d",&t,&n)&&(t||n))
    {
        flag=false;
        for(i=1; i<=n; i++)
            scanf("%d",&x[i]);
        sort(x+1,x+n+1,cmp);
        printf("Sums of %d:\n",t);
        dfs(0,1,0);
        if(!flag) printf("NONE\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator