nyoj43 24 Point game(DFS)

24 Point game

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 5
描述

There is a game which is called 24 Point game.

In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn't have any other operator except plus,minus,multiply,divide and the brackets. 

e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested. 

Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。

输入
The input has multicases and each case contains one line
The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.
Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,the second integers N(0<=N<=100) is the number which the value of the expression should be.
Then,the followed M integer is the given numbers. All the given numbers is non-negative and less than 100
输出
For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
样例输入
2
4 24 3 3 8 8
3 24 8 3 3
样例输出
Yes
No
来源
经典改编
上传者
张云聪


感觉非常经典的一道深搜题。昨天下午自己起初给sum定义了一个初始值0。后来发现第二个测试用例不对。。。那时候实在是不知道哪里错了

以至于昨天晚上做梦都梦到自己AC了这道题 哈哈尴尬尴尬

我自己觉得自己的方法没有错。于是今天早上就百度搜搜,发现没人和我的思想一样大哭大哭....别人都说是编程之美这本书上的。。可是我也没看过

他们的思想都是在一个长度为n的数组中找到任意两个数,然后求和,然后把和再插入数组中,同时n--。直到n=1.

o(︶︿︶)o ,我就只好关闭了百度。看着自己的程序慢慢调试,并且输出每一个可能的结果值。。后来还真被我发现了。。因为我初始的sum为0的缘故,

如果只有一个元素5,本来该只有一个结果的。。可是由于我的原因会出现0-5=-5,0+5=5,0*5=0,0/5=0。。。。傻了傻了。。

于是就又改了下AC了 。。嘿嘿。看来我的梦还是挺灵验的。。

看代码吧:

#include <stdio.h>
#include <math.h>
#include <string.h>
int flag,n;
double a[10],aim;//double型的把 因为相除嘛 难免会造成精度损失
int vis[10];
void dfs(double sum)
{
	int cnt=0;
	for(int i=0;i<n;i++)
	if(vis[i])
	cnt++;
	if(cnt==n&&fabs(sum-aim)<0.0000001)//一个判断的条件。当所有元素都用上。而且sum和aim相差很小
	{
		flag=1;
		return ;
	}
	for(int i=0;i<n;i++)
	{
		if(!vis[i]&&!flag)
		{
			vis[i]=1;
			dfs(sum+a[i]);//
			dfs(sum-a[i]);//
			dfs(sum*a[i]);//
			dfs(sum/a[i]);//
			dfs(a[i]/sum);//
			dfs(a[i]-sum);//对于这个数sum只能有6种情况
			vis[i]=0;
		}
	}
}
int main()
{
	int ncase;
	scanf("%d",&ncase);
	while(ncase--)
	{
		scanf("%d %lf",&n,&aim);
		for(int i=0;i<n;i++)
		scanf("%lf",&a[i]);
		flag=0;
		for(int i=0;i<n;i++)//
		{
			memset(vis,0,sizeof(vis));
			vis[i]=1;
			if(!flag)
			dfs(a[i]);//起初的sum不应该为0,应该是数组中的某一个数、、
		}//起初我没有上面这个for循环,只有一个dfs(0);华丽丽的wa...
		if(flag)
		printf("Yes\n");
		else
		printf("No\n");
	}
	return 0;
}


update:在杭电上做了一道题  今天仔细想了想 这道题我的做法始终还是有些欠缺,即使能在nyoj上A了。

所以读者自行取舍。我还是忽略了 比如1 13 5 9 我的程序是运行不出来的。因为我的程序在运算的时候一定要和当前的结果有关。

所以如果想学习新知识看我在杭电上的题解把点击打开链接这两道题一样,也当巩固一下知识。


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值