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

Re:为什么这样还不过呢

Posted by PC0400322032 at 2004-08-13 23:24:08 on Problem 1348
In Reply To:Re:哦,原来如此,谢谢。我回来再看看 Posted by:t4 at 2004-08-13 17:38:50
#include <stdio.h>
int v[5],a[6];
bool dfs(int k,int m)
{
	if(k==4)
	{
		if(m==a[4])
			return 1;
		else
			return 0;
	}
	for(int i=0;i<4;i++)
		if(v[i]==0)
		{
			v[i] = 1;
			if(dfs(k+1,m*a[i]))
				return 1;
			if(dfs(k+1,m+a[i]))
				return 1;
			if(dfs(k+1,m-a[i]))
				return 1;
			if(a[i]!=0)
			{
				if(dfs(k+1,m/a[i]))
					return 1;
			}
			///////////////////
			if(m!=0)
			{
				if(dfs(k+1,a[i]/m))
					return 1;
			}
			//////////////////
			v[i] = 0;
		}
	return 0;
}
bool done()
{
	for(int i=0;i<4;i++)
	{
		v[i] = 1;
		if(dfs(1,a[i]))
			return 1;
	}
	return 0;
}
void main()
{
	int i;
	while(1)
	{
		scanf("%d",&a[0]);
			if(a[0]<0)
				break;
		v[0] = 0;
		for(i=1;i<5;i++)
		{
			scanf("%d",&a[i]);
			v[i] = 0;
		}
		for(i=0;i<5;i++)
			printf("%d ",a[i]);
		if(done())
			printf("OK!\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