include "stdafx.h"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include<map>
#include<deque>
#include<stack>
using namespace std; bool isZero(double d)
{
return fabs(d) < 1e-6;
}
stack<char> st;
bool Count24(vector<float> nums,int n)
{
if (n == 1)
{
if (isZero(nums[0]- 24))
{
// cout << "isZero(nums[0]) - 24::" << isZero(nums[0]) - 24 << endl;
return true;
}
else
{
return false;
}
}
else
{
vector<float>temp(nums.size()-1,0);
for (int i = 2; i < nums.size(); i++)
{
temp[i - 1] = nums[i];
} temp[0] = nums[0] + nums[1];
st.push('+');
if (Count24(temp, n-1) == true) {return true;}
st.pop(); temp[0] = nums[0] - nums[1];
st.push('-');
if (Count24(temp, n - 1) == true) { return true; }
st.pop(); temp[0] = nums[0] * nums[1];
st.push('*');
if (Count24(temp, n - 1) == true) { return true; }
st.pop(); temp[0] = nums[0] / nums[1];
st.push('/');
if (Count24(temp, n - 1) == true) { return true; }
st.pop();
return false;
} return false;
} int main()
{
//cout << 'J'-11 << endl;
//cout << 'Q'-12 << endl;
//cout << 'K'-13 << endl;
//cout << 'A'-1 << endl; string str1, str2, str3, str4;
string str;
while (true)
{
vector<float> nums;
bool result = true;
for (int i = 0; i < 4; i++)
{
cin >> str;
if (str == "joker" || str == "JOKER")
{
result = false;
break;
}
else
{
switch (str[0])
{
case 'J':nums.push_back(str[0]-63); break;
case 'Q':nums.push_back(str[0] - 69); break;
case 'K':nums.push_back(str[0] - 62); break;
case 'A':nums.push_back(str[0] - 64); break;
default:
nums.push_back(str[0] - 48);
break;
}
}
}
if (result == false)
{
cout << "ERROR" << endl;
}
else
{
bool re = false;
sort(nums.begin(), nums.end());
// cout << "num的大小:" << nums.size() << endl;
// for (float f : nums)cout << f << endl;
do {
if (Count24(nums, 4))
{
stack<char>temp;
re = true;
while (!st.empty())
{
temp.push(st.top());
st.pop();
}
for (int i = 0; i < nums.size()-1; i++)
{
cout << nums[i] << temp.top() ;
temp.pop();
}
cout << nums[nums.size() - 1] << endl;
break;
};
} while (next_permutation(nums.begin(),nums.end()));
if (re == false)
{
cout << "NONE" << endl;
} } } return 0;
}

计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:的更多相关文章

  1. 加载 AssetBundle 的四种方法

    [加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...

  2. VUE 动态加载组件的四种方式

    动态加载组件的四种方式: 1.使用import导入组件,可以获取到组件 var name = 'system'; var myComponent =() => import('../compon ...

  3. PHP自动加载SPL的四种处理方式

    libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loadi ...

  4. 判断一个String中是否有指定字符或字符串

    String test=“qwer”; if (test.contains("个we")){ do; }

  5. Oracle中按规定的字符截取字符串

    CREATE OR REPLACE FUNCTION "F_SPLIT" (p_str IN CLOB, p_delimiter IN VARCHAR2) RETURN ty_st ...

  6. 【转载】 C#中PadRight函数以特定字符在字符串结尾补足位数

    在C#开发过程中字符串String类处理过程中,有时字符串长度不够时,需要在右侧侧指定特定的字符来补足字符串长度,此时可以使用String类下的PadRight方法对字符串结尾按特定的字符补足位数.M ...

  7. Java中byte与16进制字符串的互换原理

    我们都知道Java中的byte是由8个bit组成的,而16进制即16中状态,它是由4个bit来表示的,因为24=16.所以我们可以把一个byte转换成两个用16进制字符,即把高4位和低4位转换成相应的 ...

  8. 四种方式实现SQLServer 分页查询

    SQLServer 的数据分页: 假设现在有这样的一张表:CREATE TABLE test( id int primary key not null identity, names varchar( ...

  9. SQL 分页查询的四种方法

    方法一 假设现在有这样的一张表: CREATE TABLE test ( id int primary key not null identity, names ) ) 然后向里面插入大约100条数据 ...

随机推荐

  1. Problem E: 零起点学算法25——判断是否直角三角形

    #include<stdio.h> int main() { int a,b,c; while(scanf("%d %d %d",&a,&b,& ...

  2. hadoop InputSplit

    /** * <code>InputSplit</code> represents the data to be processed by an * individual {@l ...

  3. python中的else子句

    在一般的语言中else子句一般是紧跟在if 子句后面,但是python语言中else子句可以不跟在if子句后面,请看下面代码: >>> for n in range(2, 10): ...

  4. 关于JS里的函数作用域链的总结

    在JavaScript中,函数的作用域链是一个很难理解的东西.这是因为JavaScript中函数的作用域链和其他语言比如C.C++中函数的作用域链相差甚远.本文详细解释了JavaScript中与函数的 ...

  5. [Functional Programming] Draw Items from One JavaScript Array to Another using a Pair ADT

    We want to be able to pick nine random cards from an array of twelve cards, but can run into problem ...

  6. ZT:有些人,活了一辈子,其实不过是认真过了一天,其余时间都在重复这一天而已

    出处:http://news.163.com/17/1011/19/D0G7UEDS0001982T.html 有些人,活了一辈子,其实不过是认真过了一天,其余时间都在重复这一天而已,也有人每天不重样 ...

  7. S3C6410+FPGA+2*RTL8211 驱动 iperf測试

    驱动也写的差点儿相同了,所以有必要測试下性能怎样?本次採用了iperf进行測试.而且对照了下s3c6410+ks8851的測试结果 1.iperf怎样交叉编译? https://iperf.fr/ 官 ...

  8. Quartz任务监听器

    在Quartz框架提供了JobListener接口,可在任务执行前.任务被拒绝及任务执行完成后实现对任务的拦截,该接口的声明如下: public interface JobListener { /** ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面快速入门 TC3

    右击添加一个PLC项,注意不要用中文   右击VISUs,添加一个视图对象   在POUs中打开MAIN,然后添加代码(定义了一个BOOL和一个INT类型变量)   工具箱中得到一个textfield ...

  10. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-Switch Case语句是否会自动跳转到下一个

    在C#中,每一个case后面必须有break,所以输出1,也就是如果a=0,则只会执行case=0的那一段,当等于1之后不会继续.   在TwinCAT中,虽然CASE语句没有break,但是实际上不 ...