计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:
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 表示大王:的更多相关文章
- 加载 AssetBundle 的四种方法
[加载 AssetBundle 的四种方法] 1.AssetBundle.LoadFromMemoryAsync(byte[] binary, uint crc = 0); 返回AssetBundle ...
- VUE 动态加载组件的四种方式
动态加载组件的四种方式: 1.使用import导入组件,可以获取到组件 var name = 'system'; var myComponent =() => import('../compon ...
- PHP自动加载SPL的四种处理方式
libs目录下有3个类文件: Test.class.php <?php class Test { public function __construct() { echo "Loadi ...
- 判断一个String中是否有指定字符或字符串
String test=“qwer”; if (test.contains("个we")){ do; }
- Oracle中按规定的字符截取字符串
CREATE OR REPLACE FUNCTION "F_SPLIT" (p_str IN CLOB, p_delimiter IN VARCHAR2) RETURN ty_st ...
- 【转载】 C#中PadRight函数以特定字符在字符串结尾补足位数
在C#开发过程中字符串String类处理过程中,有时字符串长度不够时,需要在右侧侧指定特定的字符来补足字符串长度,此时可以使用String类下的PadRight方法对字符串结尾按特定的字符补足位数.M ...
- Java中byte与16进制字符串的互换原理
我们都知道Java中的byte是由8个bit组成的,而16进制即16中状态,它是由4个bit来表示的,因为24=16.所以我们可以把一个byte转换成两个用16进制字符,即把高4位和低4位转换成相应的 ...
- 四种方式实现SQLServer 分页查询
SQLServer 的数据分页: 假设现在有这样的一张表:CREATE TABLE test( id int primary key not null identity, names varchar( ...
- SQL 分页查询的四种方法
方法一 假设现在有这样的一张表: CREATE TABLE test ( id int primary key not null identity, names ) ) 然后向里面插入大约100条数据 ...
随机推荐
- hyxzc_背包九讲课件
10 1 1 1 5 5 7 9 //体积 5 5 1 5 3 5 1//价值 01 完全 多重 分组 有依赖性 ... ------------------------------------- ...
- delphi 获取USB口拔出和插入的状态
unit USBDeviceNotify;//USB Device arrival or removeinterface uses Windows, Messages, SysUtils, Clas ...
- BigDecimal的精度舍入模式详解
BigDecimal舍入模式介绍: 舍入模式在java.math.RoundingMode 里面: RoundingMode.CEILING :向正无限大方向舍入的舍入模式.如果结果为正,则舍入行为类 ...
- Render Texture coordinates
https://docs.unity3d.com/550/Documentation/Manual/SL-PlatformDifferences.html Render Texture coordin ...
- solr6.6 solrJ索引富文本(word/pdf)文件
1.文件配置 在core下面新建lib文件夹,存放相关的jar包,如图所示: 修改solrconfig.xml <lib dir="${solr.install.dir:../../. ...
- scrapy-splash抓取动态数据例子十三
一.介绍 本例子用scrapy-splash通过搜狗搜索引擎,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 ...
- HAWQ技术解析(十八) —— 问题排查
(原文地址:http://hawq.incubator.apache.org/docs/userguide/2.1.0.0-incubating/troubleshooting/Troubleshoo ...
- vim删除文本文件中末行^M
^M字符的来历和作用:在DOS/Windows里,文本文件的换行符为\r\n,而在*nix系统里则为\n,所以DOS/Windows里编辑过的文本文件到了*nix里,每一行都多了个^M.所以^M只是一 ...
- Netty组件介绍(转)
http://www.tuicool.com/articles/mEJvYb 为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架构中是怎么协调工作的. ...
- Oracle database wrc运行报错ORA-15557
[oracle@host capture]$ wrc system/oracle@db1 REPLAYDIR=/home/oracle/cap_dir/ Workload Replay Client: ...