zoukankan      html  css  js  c++  java
  • 益智游戏

    Description

    李老师最近开发了一项关于训练学生心算能力的益智游戏,游戏很简单,就是随机给定4个1到9之间的自然数,要求判断能否对这4个自然数进行适当的算术运算,使运算结果等于24。
      你可以使用的运算只有:+,-,,/,您还可以使用()来改变运算顺序。注意:所有的中间结果须是整数,所以一些除法运算是不允许的(例如,(22)/4是合法的,2*(2/4)是不合法的)。下面我们给出一个游戏的具体例子:
      若给出的4个自然数是:1、2、3、7,则一种可能的解答是1+2+37=24。计算过程:
      2+1=3
      7
    3=21
      21+3=24
      不要求输出具体计算过程,现在请你编写实现这个游戏加强版的程序(所谓加强版,就是要对多组数据逐一进行判断,具体输入输出及要求如下)。

    Input

    共有N+1行,第一行只有一个正整数N,表示要判断数据共有N组(1=< N <=10),接下来N行,每行有4个1到9之间的自然数(数据与数据之间用空格隔开)。

    Output

    共有N行,第K行对应于第K组4个1到9之间的自然数是否能用适当的算术运算使运算结果等于24,如果能,则输出1,如果不能则输出0。

    Sample Input
    2
    1 2 3 7
    1 1 1 1

    Sample Output
    1
    0

    .
    .
    .
    .
    .
    分析
    直接深搜
    注意情况

    .
    .
    .
    .
    .
    程序:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    bool bz=false,b[20];
    int n,a[20];
    
    void dfs(int sum,int tj)
    {
    	if (tj==4)
    	{
    		if (sum==24) bz=true;
    		return;
    	}
    	
    	for (int i=1;i<=4;i++)
    		if (b[i]==false)
    		{
    			b[i]=true;
    			dfs(sum+a[i],tj+1);
    			dfs(sum-a[i],tj+1);
    			dfs(sum*a[i],tj+1);
    			if (sum%a[i]==0) dfs(sum/a[i],tj+1);
    			dfs(a[i]-sum,tj+1);
    			b[i]=false;
    		}
    }
    
    int main()
    {
    	freopen("game.in","r",stdin);
    	freopen("game.out","w",stdout);
    	scanf("%d",&n);
    	for (int i=1;i<=n;i++)
    	{
    		for (int j=1;j<=4;j++)
    			scanf("%d",&a[j]);
    		bz=false;
    		memset(b,false,sizeof(b));
    		for (int j=1;j<=4;j++)
    			dfs(a[j],1);
    		if (bz==true) printf("1
    "); else printf("0
    ");
    	}
    	fclose(stdin);
    	fclose(stdout);
    	return 0;
    }
    
  • 相关阅读:
    images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.
    算法报告
    word2vec_basic.py
    Softmax_function
    Convex combination
    函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积
    world embedding 嵌入
    dump json 显示中文问题
    dump json 显示中文问题
    perl $d = encode_utf8($r); $f = decode_json($d)
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/11094926.html
Copyright © 2011-2022 走看看