24点运算
OJ 地址:24点运算

{
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;
}