计算 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条数据 ...
随机推荐
- Scala学习总结
1)将Array转化为String,toStrings()方法应该是序列化了的. scala> val args = Array("Hello", "world&q ...
- Python的编码注释# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-的主要作用是指定文件编码为utf-8, 因为一般默认的是ASCII码,如果要在文件里面写中文,运行时会出现乱码,加上这句之后会把文件编码强制转换为utf-8 ...
- Centos7.3 bbc tools安装
http://blog.csdn.net/orangleliu/article/details/54099528 更新到最新 CentOS 7.3 1611 yum update -y cat /et ...
- SQL SERVER 常用命令
红色为常用 0.row_number() over 和数据组合sale/cnt select *,row_number() over(order by productname) as rownumbe ...
- Social regularizations
trust-aware :如何从隐式信任中导出显示信任.链接预测就是搞这一方面的么? 和类似谱聚类的拉普拉斯矩阵结合在一起,没怎么看.
- Word中插入带公式的Visio注意事项
有时候发现,有的公式显示的间距特别大,那么在word中右键打开Visio,改好后,保存了,word里还是那样. 因为你需要吧改好的另存为原来的visio文件(名字.位置要一样,就是说替换原来的文件), ...
- Kubernetes环境下调整WebLogic JVM基本参数
基于Kubernetes启动WebLogic后,发现JVM的最大heap size一直在700多M左右,通过 kubectl logs 察看pod启动状态,发现日志中并没有-Xms和-Xmx参数.日志 ...
- Coherence装载数据的研究-PreloadRequest
最近给客户准备培训,看到Coherence可以通过三种方式批量加载数据,分别是: Custom application InvocableMap - PreloadRequest Invocation ...
- iOS:Masonry 英文原档介绍
Masonry 英文原档介绍: Masonry is still actively maintained, we are committed to fixing bugs and merging go ...
- [Javascript] Conditionally spread entries to a JavaScript object
In JavaScript, we often end up composing one object out of several other objects. Luckily there's a ...