• 34- 24 Point game


    http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=43

                      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 <cstring>
    #include <cmath>
    using namespace std;
    double a[10];
    int visit[10];
    int n, ans;
    
    int dfs(double sum, int ct){  //注意sum是中间值,可能为小数 
    	if(ct == n){
    		if(fabs(sum - ans) < 1e-6){   //fabs()判断小数更准确 
    			return 1;
    		}
    		else{
    			return 0;
    		}
    	}
    	for(int i = 0; i < n; i++){
    		if(visit[i] == 0){
    			visit[i] = 1;
    			int flag = 0;
    			flag = dfs(sum + a[i], ct + 1); if(flag) return 1;  //每个dfs应该接收返回值,避免后续计算,同时要传递返回到mian里面 
    			flag = dfs(sum - a[i], ct + 1); if(flag) return 1;
    			flag = dfs(a[i] - sum, ct + 1); if(flag) return 1;  //减法,除法要考虑顺序,出发要考虑除零的情况 
    			flag = dfs(sum * a[i], ct + 1); if(flag) return 1;
    			if(a[i]){
    				flag = dfs(sum / a[i], ct + 1); if(flag) return 1;
    			}
    			if(sum){
    				flag = dfs(a[i] / sum, ct + 1); if(flag) return 1;
    			}
    			visit[i] = 0;
    		}
    	}
    	return 0;
    }
    
    int main(){
    	int t;
    	cin >> t;
    	while(t--){
    		cin >> n >> ans;
    		for(int i = 0; i < n; i++){
    			cin >> a[i];
    		}
    		int flag = 0;		
    		for(int i = 0; i < n; i++){
    			memset(visit, 0, sizeof(visit)); //必须用这个,因为dfs有执行过程用了return,导致找到结果后直接退出,并未将visit还原 
    			visit[i] = 1;
    			if(dfs(a[i], 1)){
    				flag = 1;
    				break;
    			}
    			visit[i] = 0;
    		}
    		if(flag)
    			cout << "Yes" << endl;
    		else
    			cout << "No" << endl; 
    	} 
    	return 0;
    } 
    

      

  • 相关阅读:
    设计与声明
    字符串匹配算法——KMP、BM、Sunday
    红黑树——原理
    Linux命令——监视相关
    资源管理
    排序算法——QuickSort、MergeSort、HeapSort(C++实现)
    智能指针——使用与实现
    进程间通信——实现
    构造/析构/赋值运算
    物理内存管理
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8661071.html
Copyright © 2020-2023  润新知