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. Saga的实现模式——观察者(Saga implementation patterns – Observer)

    https://lostechies.com/jimmybogard/2013/03/11/saga-implementation-patterns-observer/ 侵删. NServiceBus ...

  2. adobe flash builder 4.6最新能用的序列号

    神key来了: 1424-4464-3877-6130-5013-5457 妈的,我两台开发机器,有一台死活激活不了,每隔一段时间就去网上搜搜激活方式已经快成为我的一个生活习惯了,可惜每次出来的都是下 ...

  3. cdev结构体及其相关函数

    一.在Linux2.6内核中一个字符设备用cdev结构来描述,其定义如下: struct cdev { struct kobject kobj; struct module *owner; //所属模 ...

  4. 解决 The &#39;InnoDB&#39; feature is disabled; you need MySQL built with &#39;InnoDB&#39; to have it working

    事由: 迁移server的时候须要操作数据库.将数据库也进行迁移,在新server中导入数据的时候提示 The 'InnoDB' feature is disabled; you need MySQL ...

  5. java web程序中项目名的更改(http://localhost:8080/)后面的名字

    在MyEclipse中的Web项目,如果想另换名称(Context-root)进行发布,可以在项目的属性中进行设置.设置路径如下: 右击项目XX(或选中项目XX,按快捷键Alt+Enter), 打开项 ...

  6. .net错误处理机制

    原地址:http://blog.csdn.net/lxbg90058/article/details/5651767 没有不出错的软件 从不出错的软件从某种程度上讲是不可能的! 和普通人的观念相反,创 ...

  7. redux-actions

    其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...

  8. rsync问题-connection refused(111)、Unknown module

    1.Connection refused (111) 启动服务:rsync --daemon --config=/etc/rsyncd.conf 2.@ERROR: Unknown module 's ...

  9. ERROR: ld.so: object '/usr/lib64/libtcmalloc.so.4' from LD_PRELOAD cannot be preloaded: ignored

    出现错误: ERROR: ld.so: object '/usr/lib64/libtcmalloc.so.4' from LD_PRELOAD cannot be preloaded: ignore ...

  10. spring自己主动装配Bean属性

    spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...