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

可能有重复数字,可以这样“打表”

Posted by 2018306666 at 2019-08-04 20:36:43 on Problem 2248
#include <cstdio>
#include <cstring>
using namespace std;

int n,cnt=0,a[20]={1},c[105][20],vis[105],ans;

void dfs(int t,int now)
{
    if(now>n||t>=ans) return;
    if(now==n)
    {
        if(t<ans)
        {
            ans=t;
            c[n][0]=t;
            for (int i=1;i<=t;i++) c[n][i]=a[i-1];
        }
        return;
    }
    memset(vis,0,sizeof(vis));
    for (int i=t-1;i>=0;i--)
        for (int j=i;j>=0;j--)
    {
        a[t]=a[i]+a[j];
        if(a[t]<a[t-1])break;
        if(vis[a[t]]==0)
        {
            vis[a[t]]=1;
            dfs(t+1,a[t]);
        }
    }
}

int main()
{
    while(++n<=100)
    {
        ans=0x3f3f3f3f;
        dfs(1,1);
    }
    while(scanf("%d",&n)&&n)
    {
        for (int i=1;i<c[n][0];i++) printf("%d ",c[n][i]);
        printf("%d\n",c[n][c[n][0]]);
    }
    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