首页 > 试题广场 >

24点运算

[编程题]24点运算
  • 热度指数:797 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

计算24点是一种扑克牌益智游戏,随机抽出4张扑克牌,通过加(+),减(-),乘(*), (/)四种运算法则计算得到整数24,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写joker表示小王,大写JOKER表示大王: 

                   3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER

本程序要求实现:输入4张牌,输出一个算式,算式的结果为24点。 

详细说明: 

1.运算只考虑加减乘除运算,没有阶乘等特殊运算符号,友情提醒,整数除法要当心 
2.牌面2~10对应的权值为2~10, JQKA权值分别为为1112131 
3.输入4张牌为字符串形式,以一个空格隔开,首尾无空格;如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算; 
4.输出的算式格式为4张牌通过+-*/四个运算符相连,中间无空格4张牌出现顺序任意,只要结果正确; 
5.输出算式的运算顺序从左至右,不包含括号,如1+2+3*4的结果为24
6.如果存在多种算式都能计算得出24,只需输出一种即可,如果无法得出24,则输出“NONE”表示无解。


输入描述:

输入4张牌为字符串形式,以一个空格隔开,首尾无空格;



输出描述:
如果输入的4张牌中包含大小王,则输出字符串“ERROR”,表示无法运算; 
示例1

输入

A A A A

输出

NONE
就模拟就行了
看了前面人没ac的代码,这边给一些可能的坑
1.看看回溯有没有完整
2.有没有输出ERROR之后又输出了NONE
3.有没有把除法运算按照double处理

本来我也是65% 我以为是1,5,5,5这种情况会出现  -1/5+5*5,但是并没有这个样例,而是我回溯的代码括号阔错了。。。

下面是AC代码
#include<bits/stdc++.h>
using namespace std;
//根据k的不同进行计算
double ff(double a,double b,int k){
    //cout<<"ff"<<endl;
    if (k == 0){
        return a+b;}
    if (k == 1){
        return a-b;}
    if (k == 2){
        return a*b;}
    if (k == 3){
        return a/b;}
}
//根据k的不同进行符号打印
string fff(int k){
    if (k == 0){
        return "+";}
    if (k == 1){
        return "-";}
    if (k == 2){
        return "*";}
    if (k == 3){
        return "/";}
}
//根据不同数字翻译牌的字符
string zz(int k){
    if (k == 13){
        return "K";}
    if (k == 12){
        return "Q";}
    if (k == 11){
        return "J";}
    if (k == 1){
        return "A";}
    else{
        return to_string(k);}
}
//计算=24的可能数字
vector<int> helper(int a,int b,int c,int d){
    //cout<<"!!"<<endl;
    for (int i=0;i<4;i++){
        //cout<<"!!!"<<endl;
        for (int j=0;j<4;j++){
            for (int k=0;k<4;k++){

                double tmp = ff(ff(ff(double(a),double(b),k),double(c),j),double(d),i);
                //cout<<i<<" "<<j<<" "<<k<<" "<<tmp<<endl;
                if (abs(tmp - 24)<0.0001)
                    return {k,j,i};

            }}}
    return {};
}
//回溯情况,这里可以优化重复情况,但是a了就算了
vector<vector<int>> pl(vector<int> &nums){
    if (nums.size()==1){
        return {{nums[0]}};}
    vector<vector<int>> tmp ;
    for (int i=0;i<nums.size();i++){
        vector<int> tt;
        for (int j=0;j<i;j++){
            tt.push_back(nums[j]);
        }
        for (int j=i+1;j<nums.size();j++){
            tt.push_back(nums[j]);
        }
        //cout<<"tt.size()"<<tt.size()<<endl;
        auto t = pl(tt);
        for (auto j: t){
            j.insert(j.begin(),nums[i]);
            tmp.push_back(j);
        }
    }
    return tmp;

}

int main(){
    vector<string> a(4,"");
    for (int i=0;i<4;i++){
        cin>>a[i];
    }
    vector<int> aa(4,0);
    int ok = 1;
    int ii = -1;
    for (auto i:a){
        ii++;
        if (i == "J"){
            aa[ii] = 11;
        }
        else if(i == "Q"){
            aa[ii] = 12;
        }
        else if(i == "K"){
            aa[ii] = 13;
        }
        else if(i == "A"){
            aa[ii] = 1;
        }
        else if(i == "joker" || i == "JOKER"){
            cout<<"ERROR";
            ok = 0;
        }
        else{
            aa[ii] = stod(i);
        }
    }

    auto pailie = pl(aa);
    int chenggong=0;
    if (ok==1){
        for (auto i:pailie){
            auto ans= helper(i[0],i[1],i[2],i[3]);
            if (ans.size()>0){
                cout<<zz(i[0])+fff(ans[0])+zz(i[1])+fff(ans[1])+zz(i[2])+fff(ans[2])+zz(i[3])<<endl;
                chenggong = 1;
                break;

            }
        }

    if (chenggong == 0){
        cout<<"NONE"<<endl;
    }
    }

}


不得不说,python用起来真香,只用考虑逻辑,不用考虑句法。。。
编辑于 2020-08-20 15:34:45 回复(0)
70%,求大神解答
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

vector<string> result;
vector<string> poker;
vector<double> num;

bool dfs(int n)
{
	if (n == 1)
	{
		/*cout << num[0] << " " << result[0] << endl;*/
		
		if (fabs(num[0] - 24.0) < 1e-6)
		{
			return true;
		}
		return  false;
	}

	double a, b;
	string expa, expb;

	for (int i = 0; i < n; ++i)
	{
		for (int j = i + 1; j < n; ++j)
		{
			a = num[i];
			b = num[j];
			num[j] = num[n - 1];

			expa = result[i];
			expb = result[j];
			result[j] = result[n - 1];

			num[i] = a + b;  result[i] = /*'(' +*/ expa + '+' + expb /*+ ')'*/; if (dfs(n - 1)) return true; 
			num[i] = a - b;  result[i] = /*'(' +*/ expa + '-' + expb /*+ ')'*/; if (dfs(n - 1)) return true; 
			num[i] = b - a;  result[i] = /*'(' +*/ expb + '-' + expa /*+ ')'*/; if (dfs(n - 1)) return true;
			num[i] = a * b;  result[i] = /*'(' +*/ expa + '*' + expb /*+ ')'*/; if (dfs(n - 1)) return true; 

			//除法分母不为0 
			if (b != 0)							
			{
				num[i] = a / b; result[i] = /*'(' +*/ expa + '/' + expb /*+ ')'*/; if (dfs(n - 1)) return true; 
			}
			if (a != 0)							 
			{
				num[i] = b / a; result[i] = /*'(' +*/ expb + '/' + expa /*+ ')'*/; if (dfs(n - 1)) return true; 
			}

			//回溯
			num[i] = a;
			num[j] = b;
			result[i] = expa;
			result[j] = expb;
		}
	}

	return false;
}

int main()
{
	string s;
	for(int i = 0; i <4; ++i)
	{
		cin >> s;
		result.push_back(s);
		poker.push_back(s);
	}

	for (auto str : poker)
	{
		if (str.length() > 2)
		{
			cout << "ERROR" << endl;
			return 0;
		}
		if (str.length() > 1)
		{
			num.push_back(10.0);
		}
		else if (str == "A")
		{
			num.push_back(1.0);
		}
		else if (str == "J")
		{
			num.push_back(11.0);
		}
		else if (str == "Q")
		{
			num.push_back(12.0);
		}
		else if (str == "K")
		{
			num.push_back(13.0);
		}
		else
		{
			num.push_back((str[0] -'0') * 1.0);
		}
		
	}

	if (dfs(4))
	{
		cout << result[0] << endl;
	}
	else
	{
		cout << "NONE" << endl;
	}
}


发表于 2021-02-09 20:15:37 回复(0)
#include <bits/stdc++.h>
 
using namespace std;
 
unordered_map<int, string> umap = {
    {1, "A"}, {2, "2"}, {3, "3"},   {4, "4"},  {5, "5"},  {6, "6"}, {7, "7"},
    {8, "8"}, {9, "9"}, {10, "10"}, {11, "J"}, {12, "Q"}, {13, "K"}};
 
void game24(vector<int>& num, string& item, int& total) {
    if (num.empty() && total == 24)
        return;
    else if (num.empty())
        return;
    for (int i = 0; i < num.size(); ++i) {
        int tmp = num[i];
        swap(num[i], *num.rbegin());
        num.pop_back();
 
        total += tmp;
        item = item + '+' + umap[tmp];
        game24(num, item, total);
        if (num.empty() && total == 24)
            return;
        item = item.substr(0, item.size() - (1 + umap[tmp].size()));
        total -= tmp;
 
        total -= tmp;
        item = item + '-' + umap[tmp];
        game24(num, item, total);
        if (num.empty() && total == 24)
            return;
        item = item.substr(0, item.size() - (1 + umap[tmp].size()));
        total += tmp;
 
        total *= tmp;
        item = item + '*' + umap[tmp];
        game24(num, item, total);
        if (num.empty() && total == 24)
            return;
        item = item.substr(0, item.size() - (1 + umap[tmp].size()));
        total /= tmp;
 
        if (total % tmp == 0) {
            total /= tmp;
            item = item + '/' + umap[tmp];
            game24(num, item, total);
            if (num.empty() && total == 24)
                return;
            item = item.substr(0, item.size() - (1 + umap[tmp].size()));
            total *= tmp;
        }
        num.push_back(tmp);
        swap(num[i], *num.rbegin());
    }
 
    return;
}
 
int main(){
    vector<string> nums(4,"");
    for(int i=0;i<4;++i){
        cin>>nums[i];
    }
    vector<int> num;
    for(int i=0; i<nums.size(); ++i){
        if(nums[i]=="A"){
            num.push_back(1);
        }else if(nums[i]=="J"){
            num.push_back(11);
        }else if(nums[i]=="Q"){
            num.push_back(12);
        }else if(nums[i]=="K"){
            num.push_back(13);
        }else if(nums[i]=="joker"||nums[i]=="JOKER"){
            cout << "ERROR" <<endl;
            return 0;
        }else{
            num.push_back(stoi(nums[i].c_str()));
        }
    }
     
    string item = "";
    for (int i = 0; i < num.size(); ++i) {
        int total = num[i];
        swap(num[i], *num.rbegin());
        num.pop_back();
        item += umap[total];
        game24(num, item, total);
        if (num.empty() && total == 24) {
            cout << item << endl;
            return 0;
        } else {
            num.push_back(total);
            swap(num[i], *num.rbegin());
            item = "";
        }
    }
    cout << "NONE" << endl;
     
    return 0;
}
类似于全排列。
发表于 2022-03-09 21:15:23 回复(0)
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

bool visited[5]={0};
int num[5]={0};
int tempnum[5]={0};
char op[5]={0};
const string card[15]={"zero","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

bool success=false;

void dfs(int level,double res)
{
    if(success)
        return;
    if(level==4&&fabs(res-24)<1e-5)
    {
        success=true;
        cout<<card[tempnum[0]];
        for(int i=1;i<4;i++)
            cout<<op[i]<<card[tempnum[i]];
        cout<<endl;
        return;
    }
    for(int i=0;i<4;i++)
    {
        if(!visited[i])
        {
            tempnum[level]=num[i];
            visited[i]=true;
            op[level]='+';
            dfs(level+1,res+num[i]);
            op[level]='-';
            dfs(level+1,res-num[i]);
            op[level]='*';
            dfs(level+1,res*num[i]);
            op[level]='/';
            dfs(level+1,res/(double)num[i]);
            tempnum[level]=0;
            visited[i]=false;
        }
    }
}

int main()
{
    string s[4];
    for(int i=0;i<4;i++)
        cin>>s[i];
    for(int i=0;i<4;i++)
    {
        if(s[i]=="joker"||s[i]=="JOKER")
        {
            cout<<"ERROR"<<endl;
            return 0;
        }
        else{
            switch(s[i][0]){
                case 'A':num[i]=1;break;
                case 'J':num[i]=11;break;
                case 'Q':num[i]=12;break;
                case 'K':num[i]=13;break;
                case '1':num[i]=10;break;
                default:num[i]=s[i][0]-'0';break;
            }
        }
    }
    
    for(int i=0;i<4;i++)
    {
        tempnum[0]=num[i];
        visited[i]=true;
        dfs(1,num[i]);
        
        //tempnum[0]=-num[i];
        //dfs(1,-num[i]);
        //样例没有负数,请不要取消注释。
        
        visited[i]=false;
        tempnum[0]=0;
    }
    if(!success)
        cout<<"NONE"<<endl;
    return 0;
}

简单的DFS。各位大佬的思路应该都是正确的,主要是输出算式的格式,并不是输出权值,而是输出原来的牌(A,J,Q,K)。题干里给出的1+2+3*4有一定误导性,让人以为是输出权值1而不是A。题也不需要考虑首项负数的情况,如-1/5+5*5,考虑了反而会出错吧。
发表于 2022-03-01 19:37:33 回复(0)
AC了50%,不知道那里错了,求解答
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
static char opt[4]  = {'+','-','*','/'};
static bool is_find = false;
int comp(vector<int>& nums,  vector<char> op)
{
    if(op.size() != 3) return 0;
    int s = nums[0];
    int j = 0;
    for(int p = 0; p < 3 || j < 3; p++)
    {
        switch(op[j])
        {
            case '+' :
                s += nums[p + 1];
                break;
            case '-' :
                s -= nums[p + 1];
                break;
            case '*':
                s *= nums[p + 1];
                break;
            case '/':
                s /= nums[p + 1];
                break;
        }
        j++;
    }
    return s;
}

void dfs(vector<int>& nums,int sum, vector<char>& op)
{
    if(op.size() > 3 || is_find) return;
    if(comp(nums, op) == sum)
    {
        cout << nums[0];
        for(int k = 0;k < op.size();k++)
        {
            cout << op[k] << nums[k+1];
        }
        is_find = true;
        return;
    }
    for(int idx = 0;idx < 3;idx++)
    {
        op.push_back(opt[idx]);
        dfs(nums, sum, op);
        op.pop_back();
    }
}


int main()
{
    unordered_map<string, int> m;
    m["A"] = 1;
    m["2"] = 2;
    m["3"] = 3;
    m["4"] = 4;
    m["5"] = 5;
    m["6"] = 6;
    m["7"] = 7;
    m["8"] = 8;
    m["9"] = 9;
    m["10"] = 10;
    m["J"] = 11;
    m["Q"] = 12;
    m["K"] = 13;
    vector<int> nums;
    string a,b,c,d;
    cin >> a >> b>> c >> d;
    vector<string> str_num = {a,b,c,d};
    for(const string& str : str_num)
    {
        if(m.find(str) == m.end())
        {
            cout << "ERROR" << endl;
            return 0;
        }
        nums.push_back(m[str]);
    }
    vector<char>op;
    dfs(nums,24, op);
    if(!is_find)
        cout << "NONE" << endl;
    return 0;
}


发表于 2021-12-20 15:27:53 回复(0)
我这个ac了,输出时需要转换回去。

#include<iostream>
#include<vector>
#include<string>
#include<map>
using namespace std;
bool alluse(vector<int> &check){
    for(auto num:check){
        if(num==0)
            return false;
    }
    return true;
}
bool dfs(vector<int> &num,vector<int> &check,vector<string> &res,int cal=0,bool ini=false){
    if(alluse(check)){
        if(cal==24)
            return true;
        else
            return false;
    }
    for(int i=0;i<num.size();++i){
        if(ini){
            check[i]=1;
            
            cal=num[i];
            res.push_back(to_string(num[i]));
            if(dfs(num,check,res,cal)){
                return true;
            }
            res.pop_back();
            
            
            
            check[i]=0;
        }
        else if(check[i]==0){
            check[i]=1;
            int temp; 
            
            temp=cal+num[i];
            res.push_back("+");            
            res.push_back(to_string(num[i]));            
            if(dfs(num,check,res,temp)){
                return true;
            }
            res.pop_back();
            res.pop_back();
            
            temp=cal-num[i];
            res.push_back("-");            
            res.push_back(to_string(num[i]));            
            if(dfs(num,check,res,temp)){
                return true;
            }
            res.pop_back();
            res.pop_back();
            
            temp=cal*num[i];
            res.push_back("*");            
            res.push_back(to_string(num[i]));            
            if(dfs(num,check,res,temp)){
                return true;
            }
            res.pop_back();
            res.pop_back();
            
                           
            temp=cal/num[i];
            res.push_back("/");            
            res.push_back(to_string(num[i]));            
            if(dfs(num,check,res,temp)){
                return true;
            }
            res.pop_back();
            res.pop_back();
            
            
            check[i]=0;
        }
    }
    return false;
}
int main(){
    string s;
    vector<string> poker;
    vector<int> num;
    map<string,string> mp;
    while(cin>>s){
        poker.push_back(s);
    }
    for(auto str:poker){
        if(str.size()>2){
            cout<<"ERROR"<<endl;
            return 0;
        }
        else if(str.size()>1){
            num.push_back(10);
        }
        else if(str=="A"){
            num.push_back(1);
            mp["1"]=str;
        }
        else if(str=="J"){
            num.push_back(11);
            mp["11"]=str;
        }
        else if(str=="Q"){
            num.push_back(12);
            mp["12"]=str;            
        }
        else if(str=="K"){
            num.push_back(13);
            mp["13"]=str;
        }
        else{
            num.push_back(str[0]-'0');
        }
    }
    vector<int> check(num.size(),0);
    vector<string> res;
    if(!dfs(num,check,res,0,true)){
        cout<<"NONE"<<endl;
        return 0;
    }
    for(auto str:res){
        string s=str;
        if(mp.count(s)!=0){
            s=mp[s];
        }
        cout<<s;
    }
    cout<<endl;
    return 0;
}


编辑于 2020-09-17 19:24:20 回复(0)
很离谱了呀,我把除法必须整除条件加上,竟然case还少了5%。运算顺序从左至右,因此无需考虑优先级,对运算符进行排列,然后计算结果,不知问题出在哪里。现在case是65%。

我貌似知道了,对于运算符需要排列,而题目中说,4张牌出现顺序任意,因此,4张牌也得排列。4 4 2 A得不到24,但是 A 2 4 4 ,A*2+4*4是可以得到24的,所以有65%的朋友,很可能是四张牌没排序。

下面是我已经修改的代码,已成功ac
#include<iostream>
using namespace std;
string s[4];// 存储四张牌 
char ope[]={'+','-','*','/'};
string sx[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
char arr[3];// 存储运算符每一次排列的结果 
int arr_s[4];// 存储四张牌每一次排列的结果 
bool FLAG=true;// 全局判断是否已计算得到24点 
// 寻找字符随对应的整数 
int search(string cache){
    for(int i=0;i<13;i++){
        if(sx[i]==cache){
            return i+1;
        }
    }
}
// 对四张牌排列去重 
bool isok(int w){     for(int i=0;i<w;i++){         if(arr_s[i]==arr_s[w]){             return false;         }     }     return true;
}
// 对四张牌进行全排列 
void dfs_s(int w){     if(w==4&&FLAG){         bool ui = true;
        int sum = search(s[arr_s[0]]);
        for(int i=0;i<3&&FLAG&&ui;i++){
            if(arr[i]=='+'){
                sum += search(s[arr_s[i+1]]);
            }
            else if(arr[i]=='-'){
                sum -= search(s[arr_s[i+1]]);
            }
            else if(arr[i]=='*'){
                sum *= search(s[arr_s[i+1]]);
            }
            else if(arr[i]=='/'){
                if(sum%search(s[arr_s[i+1]])==0){
                    sum /= search(s[arr_s[i+1]]);
                }
                else{
                    ui = false;
                }
            }
        }
        if(sum==24&&ui&&FLAG){
            FLAG=false;
        }
        return ;     }     for(int i=0;i<4&&FLAG;i++){         arr_s[w] = i;         if(isok(w)){             dfs_s(w+1);         }     }
}
// 对运算符进行部分排列 
void dfs(int w){
    if(w==3&&FLAG){
        dfs_s(0);
        return ;
    }
    for(int i=0;i<4&&FLAG;i++){
        arr[w]=ope[i];
        dfs(w+1);
    }
}
int main()
{
    bool flag = true;
    for(int i=0;i<4;i++){
        string str;
        cin >> str;
        s[i] = str;
        if(s[i]=="joker"||s[i]=="JOKER"){
            flag = false;
        }
    }
    if(flag){
        dfs(0);
        if(FLAG){
            cout << "NONE";
        }
        else{
            cout << s[arr_s[0]]+arr[0]+s[arr_s[1]]+arr[1]+s[arr_s[2]]+arr[2]+s[arr_s[3]];
        }
    }
    else{
        cout << "ERROR";
    }
    return 0;
}


编辑于 2020-09-07 13:03:08 回复(1)
60% 就过不去了.
感觉很奇怪,第五条内容说了顺序是按左到右算,说明不用考虑运算符得优先级,同时应该也不用担心类似4 / (1 - 2/3) = 12 这种情况一个数除以分数。
所以我的逻辑是递归回溯出了所有得情况,按数学的思路就是第一次四个数里选一个数,然后第二次三个数里选一个进行四个运算....按这个思路走下去,最后尽然不能ac。
import java.util.*;
public class Main {
    static boolean exist;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] strs = str.trim().split(" ");
        exist = false;
        boolean flag = false;
        int[] nums = new int[strs.length];
        for (int i = 0; i < strs.length; i++) {
            if ("joker".equals(strs[i])) {
                flag = true;
                break;
            } else if ("JOKER".equals(strs[i])) {
                flag = true;
                break;
            }
            try {
                int temp = Integer.parseInt(strs[i]);
                nums[i] = temp;
            } catch (NumberFormatException e) {
                int temp = 0;
                if ("J".equals(strs[i])) {
                    temp = 11;
                } else if ("Q".equals(strs[i])) {
                    temp = 12;
                } else if ("K".equals(strs[i])) {
                    temp = 13;
                } else if ("A".equals(strs[i])) {
                    temp = 1;
                }
                nums[i] = temp;
            }
        }
        if (flag) {
            System.out.println("ERROR");
        } else {
                        //4个数里选1个
            for(int i = 0; i < nums.length; i++) {
                if (!exist) {
                    int temp = nums[i];
                    nums[i] = -1;
                    new Main().dfs(nums,temp,1,String.valueOf(temp));
                    //一轮结束数字要回溯
                                        nums[i] = temp;
                } else {
                    break;
                }
            }
            if (!exist) {
                System.out.println("NONE");
            }
        }
    }

    public void dfs(int[] nums,double current,int stopIndexOf,String str) {
        if (stopIndexOf == 4) {
            if (!exist && Math.abs(current - 24) < 1e-6) {
                System.out.println(str);
                exist = true;
            }
            return;
        }
        int temp;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == -1) {
                continue;
            }
            if (!exist) {
                temp = nums[i];
                nums[i] = -1;
                dfs(nums,current + temp,stopIndexOf + 1,str + "+" + temp);
                dfs(nums,current - temp,stopIndexOf + 1,str + "-" + temp);
                dfs(nums,current * temp,stopIndexOf + 1,str + "*" + temp);
                dfs(nums,current / temp,stopIndexOf + 1,str + "/" + temp);
                nums[i] = temp;
            } {
                return;
            }
        }
    }
}


编辑于 2020-08-20 14:49:00 回复(0)
65%通过,不知道问题在哪....
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> split(string str, string xx)
{
    string::size_type pos = 0;
    vector<string> result;
    str += xx;
    string::size_type size = str.size();
    for (string::size_type i = 0; i < size; i++)
    {
        pos = str.find(xx, i);
        if (pos < size)
        {
            result.push_back(str.substr(i, pos - i));
            i = pos + xx.size() - 1;
        }
    }
    return result;
}

vector<string> getOperators(vector<int> nums)
{
    vector<string> result;
    double tempResult = 0;
    int tempOp = 0;
    for (int a = 0; a < 4; a++)
        for (int b = 0; b < 4; b++)
            for (int c = 0; c < 4; c++)
            {
                tempResult = nums[0];
                for (string::size_type i = 1; i < nums.size(); i++)
                {
                    if (i == 1) tempOp = a;
                    if (i == 2) tempOp = b;
                    if (i == 3) tempOp = c;
                    switch (tempOp)
                    {
                    case 0: tempResult += nums[i]; result.push_back("+"); break;
                    case 1: tempResult -= nums[i]; result.push_back("-"); break;
                    case 2: tempResult *= nums[i]; result.push_back("*"); break;
                    case 3: tempResult /= nums[i]; result.push_back("/"); break;
                    }
                }
                if (tempResult == 24.00f)
                {
                    return result;
                }
                result.clear();
            }
    return result;
}

int main()
{
    string input;
    getline(cin, input);
    vector<string> inputSplit = split(input, string(" "));
    vector<int> result;
    for (auto nowStr : inputSplit)
    {
        if (nowStr.c_str()[0] >= '2' && nowStr.c_str()[0] <= '9')
            result.push_back(nowStr.c_str()[0] - '0');
        else if (nowStr.compare("10") == 0)
            result.push_back(10);
        else if (nowStr.compare("J") == 0)
            result.push_back(11);
        else if (nowStr.compare("Q") == 0)
            result.push_back(12);
        else if (nowStr.compare("K") == 0)
            result.push_back(13);
        else if (nowStr.compare("A") == 0)
            result.push_back(1);
        else if (nowStr.compare("JOKER") == 0 || nowStr.compare("joker") == 0)
        {
            cout << "ERROR";
            return 0;
        }
        else
        {
            cout << "ERROR";
            return 0;
        }
    }
    auto test = getOperators(result);
    if (!test.empty())
    {
        cout << inputSplit[0];
        for (vector<int>::size_type i = 1; i < inputSplit.size(); i++)
        {
            cout << test[i - 1] << inputSplit[i];
        }
    }
    else
    {
        cout << "NONE";
    }
}


发表于 2020-08-20 11:28:22 回复(1)