每日一练之24点运算

24点运算

OJ 地址:24点运算
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200825145020644.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMD在这里插入图片描述


#include <iostream>
#include <map>
using namespace std;
 
char oper[4]={'+','-','*','/'};
bool onesolution=false;
 
bool solve(int *p,char *op,int pos)
{
    int n=0;
    int temp;
    if(p[pos]==24)
    {
        if(pos==3)
        {
            onesolution=true;
            return true;
        }
        else return false;
    }
    else if(p[pos]!=24)
    {
        if(pos==3)
            return false;
    }
    while(n<4)
    {
        op[pos]=oper[n];
        temp=p[pos+1];
        if(n==0)
            p[pos+1]=p[pos]+p[pos+1];
        else if(n==1)
            p[pos+1]=p[pos]-p[pos+1];
        else if(n==2)
            p[pos+1]=p[pos]*p[pos+1];
        else
        {
            if(p[pos+1]==0||p[pos]%p[pos+1]!=0)//
                return false;
            else p[pos+1]=p[pos]/p[pos+1];
        }
 
        if(!solve(p,op,pos+1))
            p[pos+1]=temp;
        if(onesolution==true)
            return true;
        n++;
    }
    return false;
}
 
void swap(int *a ,int *b)
{
    int m ;
    m = *a;
    *a = *b;
    *b = m;
}
 
bool perm(int *list,char *op,int pos,int k, int m )
{
    int i;
    int list2[4];
    if(k >m)
    {
        for(i=0;i<4;i++)
            list2[i]=list[i];
        if(solve(list2,op,pos))
        {
            return true;
        }
    }
    else
    {
        for(i = k ; i <=m;i++)
        {
            swap(&list[k],&list[i]);
            //cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<" "<<list[3]<<endl;
            if(perm(list,op,pos,k+1,m))
            {
                return true;
            }
 
            swap(&list[k],&list[i]);
        }
    }
 
    return false;
}
struct mapcd
{
    string str;
    int dig;
};
int main()
{
    map<string,int> map1;
    map1["1"]=1;map1["A"]=1;map1["2"]=2;map1["3"]=3;map1["4"]=4;map1["5"]=5;map1["6"]=6;map1["7"]=7;
    map1["8"]=8;map1["9"]=9;map1["10"]=10;map1["J"]=11;map1["Q"]=12;map1["K"]=13;
    string c[4];
    int p[4];
    char op[3];
    int i,flag=0,j;
    while(cin>>c[0])
    {
        flag=0;
        onesolution=false;
        if(c[0].length()==1)
            {
                p[0]=map1[c[0]];
                //cout<<p[i]<<" ";
            }
        else flag=1;
        for(i=1;i<4;i++)
        {
            cin>>c[i];
            if(c[i].length()==1)
            {
                p[i]=map1[c[i]];
                //cout<<p[i]<<" ";
            }
            else flag=1;
        }
        if(flag==1)
            cout<<"ERROR"<<endl;
        else if(!perm(p,op,0,0,3))
            cout<<"NONE"<<endl;
        else
        {
 
            for(i=0;i<3;i++)
            {
                for(j=0;j<4;j++)
                    if(p[i]==map1[c[j]])
                    {
                        cout<<c[j]<<op[i];
                        break;
                    }
 
            }
            for(j=0;j<4;j++)
                    if(p[i]==map1[c[j]])
                    {
                        cout<<c[j];
                        break;
                    }
            cout<<endl;
        }
    }
 
    return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值