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

感谢1 1 数据,不想用stl

Posted by Wolfy at 2018-01-27 20:25:58 on Problem 3187
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int a[15],vis[15],sum[20],n,final,flag,head,temp[20];
void dfs(int k)
{
    if(k==n)
    {
        int i,j; //i=3
        for(i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[k]=i;
                sum[k-2]=a[k-1]+a[k];
                break;
            }
        }
        for(i=0;i<n-1;i++) temp[i]=sum[i];
        i=n-1;
        while((i--)>1) //i=2
        {
            for(j=0;j<i;j++) temp[j]=temp[j]+temp[j+1];
        }
        if(temp[0]==final)
        {
            for(i=1;i<n;i++) printf("%d ",a[i]);
            printf("%d\n",a[i]);
            flag=1;
        }
    }
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                a[k]=i;
                if(k!=1) sum[k-2]=a[k-1]+a[k];
                vis[i]=1;
                dfs(k+1);
                if(flag) break;
                vis[i]=0;
                sum[k-2]-=a[k];
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&final))
    {
        if(n==1&&final==1) printf("1\n");
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        a[0]=0;
        flag=0;
        head=2;
        dfs(1);
    }
}

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