24 Game

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through */+-() to get the value of 24.

Example 1:

Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24

Example 2:

Input: [1, 2, 1, 2]
Output: False

Note:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.


列出四个数的全排列,对每一个排列进行所有可能的运算(三个位置的运算符加减乘除有4*4*4=64种可能,插入括号有6种方式),检查运算结果是否为24。


#include<iostream>
#include<vector>
#include <cmath>
#include <algorithm>
using namespace std;
class Solution {
public:
	vector<double> operands;
    	bool judgePoint24(vector<int>& nums) {
    		for (int i = 0 ;i < 4; i++)
    			operands.push_back(nums[i]);
        	for (int i = 0; i < 24; i++) {
			for (int x = 1; x <= 4; x++)
			for (int y = 1; y <= 4; y++)
			for (int z = 1; z <= 4; z++) {
				double left,middle,right,result;
				left=compute(x, operands[0], operands[1]);
				middle=compute(y, operands[1],operands[2]);
				right=compute(z,operands[2], operands[3]);
				
				result=compute(y,left,operands[2]);
				result=compute(z,result,operands[3]);
				if (fabs(result-24)<0.001) return true;//a?b?c?d

				result=compute(y, left,right);
				if (fabs(result-24)<0.001) return true;//(a?b)?(c?d)

				result=compute(x, operands[0], middle);
				result=compute(z, result, operands[3]);//(a?(b?c))?d
				if (fabs(result-24)<0.001) return true;

				result=compute(z, middle,operands[3]);
                		result=compute(x, operands[0],result);//a?((b?c)?d)
                		if (fabs(result-24)<0.001) return true;

				result= compute(y,left,right);
				if (fabs(result-24)<0.001) return true;//a?b?(c?d)

				result=compute(y,operands[1],right);
				result=compute(x,operands[0],result);
				if (fabs(result-24)<0.001) return true;//a?(b?(c?d))
			}
			next_permutation(operands.begin(),operands.end());
		}
		return false;
    	}
	double compute(int oper, double & a, double& b) {
		if (oper==1) return a+b;
		if (oper==2) return a-b;
		if (oper==3) return a*b;
		if (oper==4) {
			if (b==0) return 0;		 
			return a/b;
		}
	}
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旅游社交小程序功能有管理员和用户。管理员有个人中心,用户管理,每日签到管理,景点推荐管理,景点分类管理,防疫查询管理,美食推荐管理,酒店推荐管理,周边推荐管理,分享圈管理,我的收藏管理,系统管理。用户可以在微信小程序上注册登录,进行每日签到,防疫查询,可以在分享圈里面进行分享自己想要分享的内容,查看和收藏景点以及美食的推荐等操作。因而具有一定的实用性。 本站后台采用Java的SSM框架进行后台管理开发,可以在浏览器上登录进行后台数据方面的管理,MySQL作为本地数据库,微信小程序用到了微信开发者工具,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得旅游社交小程序管理工作系统化、规范化。 管理员可以管理用户信息,可以对用户信息添加修改删除。管理员可以对景点推荐信息进行添加修改删除操作。管理员可以对分享圈信息进行添加,修改,删除操作。管理员可以对美食推荐信息进行添加,修改,删除操作。管理员可以对酒店推荐信息进行添加,修改,删除操作。管理员可以对周边推荐信息进行添加,修改,删除操作。 小程序用户是需要注册才可以进行登录的,登录后在首页可以查看相关信息,并且下面导航可以点击到其他功能模块。在小程序里点击我的,会出现关于我的界面,在这里可以修改个人信息,以及可以点击其他功能模块。用户想要把一些信息分享到分享圈的时候,可以点击新增,然后输入自己想要分享的信息就可以进行分享圈的操作。用户可以在景点推荐里面进行收藏和评论等操作。用户可以在美食推荐模块搜索和查看美食推荐的相关信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值