当前位置:网站首页>ACM. HJ89 24点运算 ●●●
ACM. HJ89 24点运算 ●●●
2022-06-23 22:12:00 【chenyfan_】
HJ89 24点运算 ●●●
描述
计算24点是一种扑克牌益智游戏,随机抽出4张扑克牌,通过加(+),减(-),乘(*), 除(/)四种运算法则计算得到整数24,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写joker表示小王,大写JOKER表示大王:
3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
本程序要求实现:输入4张牌,输出一个算式,算式的结果为24点。
详细说明:
1.运算只考虑加减乘除运算,没有阶乘等特殊运算符号,没有括号,友情提醒,整数除法要当心,是属于整除,比如2/3=0,3/2=1;
2.牌面 2-10 对应的权值为 2-10 , J、Q、K、A权值分别为为11、12、13、1;
3.输入4张牌为字符串形式,以一个空格隔开,首尾无空格;如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算;
4.输出的算式格式为4张牌通过+ - * / 四个运算符相连,中间无空格,4张牌出现顺序任意,只要结果正确;
5.输出算式的运算顺序从左至右,不包含括号,如 1+2+3*4 的结果为24,2 A 9 A不能变为 (2+1) * (9-1) =24
6.如果存在多种算式都能计算得出24,只需输出一种即可,如果无法得出24,则输出“NONE”表示无解。
7.因为都是扑克牌,不存在单个牌为0的情况,且没有括号运算,除数(即分母)的数字不可能为0。
数据范围:一行由4张牌组成的字符串
输入描述:
输入4张牌为字符串形式,以一个空格隔开,首尾无空格;
输出描述:
输出怎么运算得到24,如果无法得出24,则输出“NONE”表示无解,如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算;
示例
输入:
4 2 K A
输出:
K-A*4/2
说明:
A+K*2-4 也是一种答案,输出任意一种即可
输入:
K Q 6 K
输出:
NONE
说明:
按一般的计算规则来看,K+K-(Q/6)=24 或 K-((Q/6)-K)=24,但是因为这个题目的运算不许有括号,所以去掉括号后变为 K+K-Q/6=26-Q/6=14/6=2 或 K-Q/6-K=1/6-K=0-K=-13,其它情况也不能运算出24点,故不存在,输出NONE
题解
1. 暴力搜索
该题与ACM. HJ67 24点游戏算法 & 679. 24 点游戏类似,但是又存在差异。
比如输出结果的运算是不存在括号的,所以没有运算符的优先级,都是从左往右进行运算,并且需要输出运算过程。
为此,我们可以使用暴力遍历进行查找,数字 num 的组合有4 * 3 * 2 * 1 = 24 种,运算符 op 的组合有 4 * 4 * 4 = 64 种,因此具有24 * 64 = 1536 种组合,我们可以用循环进行遍历。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int TARGET = 24;
string convert(int num){
// 将数字转换为字符串
if(num >= 2 && num <= 10) return to_string(num);
if(num == 1) return "A";
if(num == 11) return "J";
if(num == 12) return "Q";
if(num == 13) return "K";
return to_string(num);
}
string convertOP(int num){
// 将数字转换为运算符字符串
if(num == 1) return "+";
if(num == 2) return "-";
if(num == 3) return "*";
if(num == 4) return "/";
return "NULL";
}
int cal(int a, int op, int b){
// 两数计算
if(op == 1) return a + b;
if(op == 2) return a - b;
if(op == 3) return a * b;
if(op == 4) return a / b;
return 1;
}
string getStr(int a, int op1, int b, int op2, int c, int op3, int d){
// 最终结果转换
return convert(a) + convertOP(op1) + convert(b) + convertOP(op2) + convert(c) + convertOP(op3) + convert(d);
}
string traversal(vector<int> & nums){
for (int i = 0; i < 4; i++){
// nums0
for(int j = 0; j < 4; ++j){
// nums1
if(j == i) continue;
for(int k = 0; k < 4; ++k){
// nums2
if(k == i || k == j) continue;
for(int l = 0; l < 4; ++l){
// nums3
if(l == i || l == j || l ==k) continue;
for(int op1 = 1; op1 <= 4; ++op1){
// op1
for(int op2 = 1; op2 <= 4; ++op2){
// op2
for(int op3 = 1; op3 <= 4; ++op3){
// op3
int sum = cal(nums[i], op1, nums[j]);
sum = cal(sum, op2, nums[k]);
sum = cal(sum, op3, nums[l]);
if(sum == TARGET) return getStr(nums[i], op1, nums[j], op2, nums[k], op3, nums[l]);
}
}
}
}
}
}
}
return "NONE";
}
int main(){
string strs[4];
vector<int> nums(4, 0);
while(cin >> strs[0] >> strs[1] >> strs[2] >> strs[3]){
for(int i = 0; i < 4; ++i){
if(strs[i][0] >= '2' && strs[i][0] <= '9'){
nums[i] = strs[i][0] - '0';
}else if(strs[i][0] == 'A'){
nums[i] = 1;
}else if(strs[i][0] == 'J'){
nums[i] = 11;
}else if(strs[i][0] == 'Q'){
nums[i] = 12;
}else if(strs[i][0] == 'K'){
nums[i] = 13;
}else if(strs[i][0] == '1'){
nums[i] = 10;
}else{
cout << "ERROR";
return 0;
}
}
cout << traversal(nums) << endl;
}
return 0;
}
边栏推荐
- AndroidKotlin全面详细类使用语法学习指南
- Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA
- Unknown character set index for field ‘255‘ received from server.
- PyQt5_QTableWidget分页单选右键菜单控件
- Postman可以集成到CI,CD流水线中做自动化接口测试吗?
- 谈谈数字化转型晓知识
- Preliminary understanding of 3D printing and laser cutting process
- Map集合的四种遍历
- kubernetes之常用核心资源对象
- The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
猜你喜欢

抖音支付十万级 TPS 流量发券实践

Why do MySQL indexes use b+ trees at the bottom? After reading this article, you can easily handle the interview.

Autofac详解

ASM文件系统 数据如何写和读数据

Summary of some indicators for evaluating and selecting the best learning model

How to write and read ASM file system data

WebService client request failed can not create a secure xmlinputfactory

嵌入式接口之TIM定时器与NVIC的STM32模板库函数的一些解释

评估和选择最佳学习模型的一些指标总结

iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
随机推荐
Several guesses about the design of Tencent conference number
BroadcastReciver 和LocalBroadcastManager区别
【Try to Hack】masscan
ORB_SLAM3环境搭建及demo演示
Unknown character set index for field ‘255‘ received from server.
FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
Activity的onSaveInstanceState回调时机
Several cases of index invalidation caused by MySQL
HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
19 MySQL optimizations commonly used in projects
laravel之任务队列
STM32-------外部中断
Some explanations of Tim timer of embedded interface and STM32 template library function of NVIC
6、STM32——串口数据收发基础
[Xilinx ax7103 microbalze Learning Notes 6] MicroBlaze custom IP core packaging experiment
The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
Postman可以集成到CI,CD流水线中做自动化接口测试吗?
To ensure the safety of groups with special difficulties, Guangzhou Civil Affairs made every effort to do a good job in the three prevention work
Unknown character set index for field ‘255‘ received from server.
AndroidKotlin全面详细类使用语法学习指南