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

大家帮忙看看我的为什么老是WA,郁闷中.......

Posted by baidu85 at 2008-02-23 15:20:46 on Problem 2362 and last updated at 2008-02-23 15:21:10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int a[16];
int status[16];
int ca,M,sum,ave,flag;

int cmp(const void* a,const void* b)
{
	return(*(int*)a-*(int*)b);
}

void DFS(int st,int now,int edge)
{
	if(edge==3)
	{
		flag=1;
		return;
	}
	else if(!flag)
	{
		int i;
		for(i=st;i>=0&&!flag;i--)
		{
			if(!status[i])
			{
				if(now+a[i]==ave)
				{
					status[i]=1;
					DFS(M-1,0,edge+1);
					status[i]=0;
				}
				else if(now+a[i]<ave)
				{
					status[i]=1;
					DFS(i,now+a[i],edge);
					status[i]=0;
				}
			}
		}
	}
}

void main()
{
	scanf("%d",&ca);
	while(ca--)
	{
		int i;
		sum=0;
		memset(status,0,sizeof(status));
		scanf("%d",&M);
		for(i=0;i<M;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		if(sum%4)
		{
			printf("no\n");
			continue;
		}
		ave=sum/4;
		qsort(a,M,sizeof(int),cmp);
		if(a[M-1]>ave)
		{
			printf("no\n");
			continue;
		}
		flag=0;
		DFS(M-1,0,0);
		if(flag)
			printf("yes\n");
		else
			printf("no\n");
	}
}

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