计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:
2017-06-27 16:18
1366 查看
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; }
相关文章推荐
- 输入一串字符串,运用输出方法计算其中大写 小写 数字 和其他字符的个数
- ACM457现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。输入 第一行只有一个整数m(m<=10),表示测试数据组数。
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
- LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面
- 从键盘输入一个带有数字的字符串,将其中所在的数字的位置输出,并把数字串小写字符变大写字符再输出
- 这段代码是看 用3 ,3 ,8, 8 四个数通过四种基本运算方式得到24这个值,没啥新意,偶用穷举做的.
- 将string字符串中的字符转为全部大写或者全部小写
- 假设用一个名为text的字符串向量存放文本文件的数据,其中的元素或者是一句话或者是一个用于表示段分隔的空字符串。将text中第一段全改为大写形式
- 计算用字符串表示的整数四则运算的值
- 从一副52张的牌中,选出四张,然后计算它们的和。其中,J、Q、K和A分别表示11、12、13和1。编写程序显示得到的和为24的选牌次数,并显示选出的牌。
- LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育
- 汇编语言: 从键盘上输入一串字符(用回车键结束,使用10号功能调用。),将其中的小写英文 字母变换为大写英文字母,其他字符保持不变。然后将变换后的字符串显示出来。
- 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。 是否AC的规则如下: 1. zoj能AC; 2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
- 对于给定的一个字符串,统计其中数字字符出现的次数。输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
- 第4章, 5.9.1输入一个字符串,把其中的大写转为小写,其他字符不变
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- JAVA--第十周作业编写之一个Teacher类负责给出算术题目,随机给出两个整数并进行运算,并判断回答者的答案是否正确;编写一个GUI类ComputerFrame,回答者可以通过GUI看到题目并给出
- 计算用字符串表示的个位数四则运算的值(栈)
- 角谷步数 你听说过角谷猜想吗? 任意的正整数,比如 5, 我们从它开始,如下规则计算: 如果是偶数,则除以2,如果是奇数,则乘以3再加1. 如此循环,最终必会得到“1” !
- java 判断字符串首字符大写(uppercase或者capital)