(NYOJ 43 24point)&&(HDU 1427 速算24点)

速算24点

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3772 Accepted Submission(s): 928


Problem Description
速算24点相信绝大多数人都玩过。就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13)。要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次)。游戏很简单,但遇到无解的情况往往让人很郁闷。你的任务就是针对每一组随机产生的四张牌,判断是否有解。我们另外规定,整个计算过程中都不能出现小数。

Input
每组输入数据占一行,给定四张牌。

Output
每一组输入数据对应一行输出。如果有解则输出"Yes",无解则输出"No"。

Sample Input
   
   
   
   
A 2 3 6 3 3 8 8

Sample Output
   
   
   
   
Yes No
/*不对 这个思路每次都对sum操作了 但是实际上不一定都操作sum  可能另外2个
相加然后再和sum操作*/
/* 
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
//#define NUM 1e-6
using namespace std;
int a[6];
int n;
bool book[6];
bool dfs(int sum,int time){
	if(time==4){
		 if(24==sum)
	      return true;
	      return false;
	}	
	 int i,j;
	 for(i=0;i<4;i++)
	{
		if(book[i]==1)
		continue;
		book[i]=1;   
		if(dfs(sum+a[i],time+1))  return true;
	 	if(dfs(sum-a[i],time+1))  return true;
		if(dfs(sum*a[i],time+1))  return true;
	 	if(dfs(a[i]-sum,time+1))  return true;
	 	if(a[i]!=0&&sum%a[i]==0){
	 		if(dfs(sum/a[i],time+1))  return true;
		 } 	
	 	if(sum!=0&&a[i]%sum==0){
	 		if(dfs(a[i]/sum,time+1))  return true;
		 }	
		 book[i]=0;
	 }	  
	 return false; 
}
inline int hehe(char* c){
	if(c[0]=='A')
	return 1;
	else if(c[0]=='J')
	return 11;
	else if(c[0]=='Q')
	return 12;
	else if(c[0]=='K')
	return 13;
	else if(strlen(c)==2)
	return 10;
	else 
	return c[0]-'0';
}
int main(){
	int t,i;
	char c[5];
	while(scanf("%s",c)==1){
		a[0]=hehe(c);
		book[0]=0;
		for(i=1;i<4;i++)
		{
			scanf("%s",c);
			a[i]=hehe(c);
			book[i]=0;
		}
		bool ok=0;
		for(i=0;i<4;i++)
		{
			book[i]=1;
			if(dfs(a[i],1))
			{
				ok=1;
				break;
			}
			book[i]=0;
		}
		if(ok)
		printf("Yes\n");
		else
		 printf("No\n");
		
	}
	return 0;
}
*/




#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
//#define NUM 1e-6
using namespace std;
int a[6];
int n;
bool dfs(int x){
	if(x==3){
		 if(24==a[x])
	      return true;
	      return false;
	}	
	 int i,j;
	 for(i=x;i<4;i++)
	 for(j=i+1;j<4;j++){
	 	int le=a[i],ri=a[j];
	 	//这里很难理解 X是指第几个  可能x=0 然后循环是到i=1,j=2
		 //结果会保存到j=2,下次递归是x+1  x=0没操作会被跳过 所以每次
		 //将a[x]的值保存到a[i] 
	 	a[i]=a[x];   //???????????  妈的!别人好聪明! 
	 	a[j]=le+ri;	   if(dfs(x+1))  return true;
	 	a[j]=le-ri; 	if(dfs(x+1))  return true;
		a[j]=le*ri;  	if(dfs(x+1))  return true;
	 	a[j]=ri-le;  	if(dfs(x+1))  return true;
	 	if(ri!=0&&le%ri==0){
	 		a[j]=le/ri;	if(dfs(x+1))  return true;
		 } 	
	 	if(le!=0&&ri%le==0){
	 		a[j]=ri/le;	if(dfs(x+1))  return true;
		 }	
		 a[i]=le;
		 a[j]=ri; 	
	 }	  
	 return false; 
}
inline int hehe(char* c){
	if(c[0]=='A')
	return 1;
	else if(c[0]=='J')
	return 11;
	else if(c[0]=='Q')
	return 12;
	else if(c[0]=='K')
	return 13;
	else if(strlen(c)==2)
	return 10;
	else 
	return c[0]-'0';
}
int main(){
	int t,i;
	char c[5];
	while(scanf("%s",c)==1){
		a[0]=hehe(c);
		for(i=1;i<4;i++)
		{
			scanf("%s",c);
			a[i]=hehe(c);
		}
		if(dfs(0))
		printf("Yes\n");
		else
		 printf("No\n");
		
	}
	return 0;
}



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

#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#define NUM 1e-6
using namespace std;
double a[6],re;
int n;
bool dfs(int x){
	if(x==n-1){
		 if(fabs(re-a[x])<NUM)
	      return true;
	      return false;
	}	
	 int i,j;
	 for(i=x;i<n;i++)
	 for(j=i+1;j<n;j++){
	 	double le=a[i],ri=a[j];
	 	a[i]=a[x];
	 	a[j]=le+ri;	   if(dfs(x+1))  return true;
	 	a[j]=le-ri; 	if(dfs(x+1))  return true;
		a[j]=le*ri;  	if(dfs(x+1))  return true;
	 	a[j]=ri-le;  	if(dfs(x+1))  return true;
	 	if(ri!=0){
	 		a[j]=le/ri;	if(dfs(x+1))  return true;
		 }	 
	 	if(le!=0){
	 			a[j]=ri/le;	if(dfs(x+1))  return true;
		 }	 	
		 a[i]=le;
		 a[j]=ri; 	
	 }	  
	 return false; 
}
int main(){
	int t,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d%lf",&n,&re);
		for(i=0;i<n;i++)
			scanf("%lf",&a[i]);	
		if(dfs(0))
		printf("Yes\n");
		else
		 printf("No\n");
		
	}
	return 0;
}


你可能感兴趣的:((NYOJ 43 24point)&&(HDU 1427 速算24点))