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 1152230301 at 2011-09-07 23:47:33 on Problem 1564
#include<iostream>
using namespace std;  

int a[15];
bool vis[15];
int t,n,flag;

void dfs(int c,int sum)
{
     int i;
     if(sum==t)
     {
                for(i=0;i<c;i++)
                {
                     if(vis[i])
                     {
                                printf("%d+",a[i]);
                     }
                    
                } 
     printf("%d\n",a[c]);
     flag=1;
    }
    else
    {
                for(i=c+1;i<n;i++)
                {

                                  if(a[i]>t-sum)
                                  continue;
                                  if(a[i]==a[i-1]&&!vis[i-1])
                                  continue;
                                  vis[i]=1;
                                  dfs(i,a[i]+sum);
                                  vis[i]=0;
                }
    }
}

int main()
{
    int i;
    int sum;
    while(scanf("%d%d",&t,&n)!=EOF&&n)
    { 
                                      flag=sum=0;
                                      for(i=0;i<n;i++)
                                      { 
                                                      scanf("%d",&a[i]);
                                                      sum+=a[i];
                                      }
                                      
                                      printf("Sums of %d:\n",t);             
                                      if(sum<t)
                                      {
                                               printf("NONE\n");
                                               continue;
                                      }
                                      if(sum==t)
                                      {
                                                 for(i=0;i<n-1;i++)
                                                 printf("%d+",a[i]);
                                                 printf("%d\n",a[n-1]);
                                                 continue;
                                      }
                                      
                                      memset(vis,0,sizeof(vis));
                                      for(i=0;i<n;i++)
                                      {
                                                      if(a[i]==a[i-1]&&i)
                                                      continue;
                                                      vis[i]=1;
                                                      dfs(i,a[i]);
                                                      vis[i]=0;
                                      }
                                      if(!flag)
                                      printf("NONE\n");//不是所有的数据之和大于t就可以找到正确的加的方法。想了半天。
    }
    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