24点游戏:
24点是一种益智游戏,24点是把4个整数(一般是正整数)通过加减乘除以及括号运算,使最后的计算结果是24的一个数学游戏,24点可以考验人的智力和数学敏感性,它能在游戏中提高人们的心算能力。
24点通常是使用扑克牌来进行游戏的,一副牌中抽去大小王后还剩下52张(如果初练也可只用1~10这40张牌),任意抽取4张牌(称为牌组),用加、减、乘、除(可加括号)把牌面上的数算成24。每张牌必须只能用一次,如抽出的牌是3、8、8、9,那么算式为(9-8)×8×3或3×8÷(9-8)或(9-8÷8)×3等。(摘自百度百科)
算法思路:
因为有4个数,所以可以用暴力搜索所有可能情况的方式。但注意4个数的组合方式有两种:
一种是用一个数和剩下的一个数进行运算,得到的结果再和下一个数运算,一个一个数的运算得到24;
另一种是用两个数运算得到一个结果,用另两个数得到第二个结果,两个结果之间再运算得到24.
我是用广搜做的,也练了一下输出方案,具体看代码。如果想看到所有的方案,把两个找到结果地方的break和return给注释掉,就可以了(会有重复的,懒得改了。。。)
代码:
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define eps 1e-6
struct node{
double ans;
int State;
int op[4];
int ch[5];
int p;
};
int a[4];
void output(node x){
char s[30]={};
int l = 14,r = 15;
s[r++] = x.ch[0]+'0';
for (int i = 1; i < 4; i++)
{
switch(x.op[i])
{
case( 0 ):s[r++]='+';s[r++]=x.ch[i]+'0';break;
case( 1 ):s[r++]='-';s[r++]=x.ch[i]+'0';break;
case( 2 ):s[l--]='-';s[l--]=x.ch[i]+'0';break;
case( 3 ):s[r++]='*';s[r++]=x.ch[i]+'0';break;
case( 4 ):s[l--]='/';s[l--]=x.ch[i]+'0';break;
case( 5 ):s[r++]='/';s[r++]=x.ch[i]+'0';break;
default:break;
}
if(i<3){
s[l--] = '(';
s[r++] = ')';
}
}
printf("一种可以的计算方案:\t");
for (int i = l+1; i < r; i++)
{
if(s[i] > '0')printf("%d",s[i]-'0');
else printf("%c",s[i]);
}printf("\n");
return ;
}
void print(char s[]){
for (int i = 0; i < 5; i++)
{
if(s[i] > '0')printf("%d",s[i]-'0');
else printf("%c",s[i]);
}
}
bool check(int s,int d,int f,int g){
int flag = 0;
double h1,h2;
double ans;
char x[6]={},y[6]={};
x[0] = y[0] = '(';
x[4] = y[4] = ')';
x[5] = y[5] = '\0';
for (int j = 0; j < 6; j++)
{
if(j==0) {h1 = s + d; x[1] = s+'0';x[2] = '+';x[3] = d+'0';}
else if(j==1) {h1 = s - d; x[1] = s+'0';x[2] = '-';x[3] = d+'0';}
else if(j==2) {h1 = d - s; x[1] = d+'0';x[2] = '-';x[3] = s+'0';}
else if(j==3) {h1 = d * s; x[1] = d+'0';x[2] = '*';x[3] = s+'0';}
else if(j==4 && s!=0) {h1 =1.0*d/s;x[1] = d+'0';x[2] = '/';x[3] = s+'0';}
else if(j==5) {h1 =1.0*s/d;x[1] = s+'0';x[2] = '/';x[3] = d+'0';}
else continue;
for (int i = 0; i < 6; i++)
{
if(i==0) {h2 = f + g; y[1] = f+'0';y[2] = '+';y[3] = g+'0'; }
else if(i==1) {h2 = f - g; y[1] = f+'0';y[2] = '-';y[3] = g+'0'; }
else if(i==2) {h2 = g - f; y[1] = g+'0';y[2] = '-';y[3] = f+'0'; }
else if(i==3) {h2 = g * f; y[1] = g+'0';y[2] = '*';y[3] = f+'0'; }
else if(i==4 && f!=0) {h2 =1.0*g/f;y[1] = g+'0';y[2] = '/';y[3] = f+'0';}
else if(i==5) {h2 =1.0*f/g;y[1] = f+'0';y[2] = '/';x[3] = g+'0';}
else continue;
for (int k = 0; k < 6; k++)
{
if(k==0)ans = h1 + h2;
else if(k==1)ans = h1 - h2;
else if(k==2)ans = h2 - h1;
else if(k==3)ans = h1 * h2;
else if(k==4 && h1!=0)ans = h2/h1;
else if(k==5 && h2!=0)ans = h1/h2;
if(fabs(ans-24.0)<eps){
printf("一种可以的计算方案:\t");
switch (k)
{
case(0):print(x);printf("+");print(y);break;
case(1):print(x);printf("-");print(y);break;
case(3):print(x);printf("*");print(y);break;
case(5):print(x);printf("/");print(y);break;
case(4):print(y);printf("/");print(x);break;
case(2):print(y);printf("-");print(x);break;
default:
break;
}
printf("\n");
flag = 1;
return 1;
}
}
}
}
if(flag)return 1;
return 0;
}
queue<node>Q;
int main(){
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF){
node st;
while(!Q.empty())Q.pop();
for (int i = 0; i < 4; i++)
{
st.ans = a[i];
st.State = 1<<i;
st.p = 0;
st.op[0] = 7;
st.ch[0] = a[i];
Q.push(st);
}
node nows,next;
int flag = 0;
while(!Q.empty()){
nows = Q.front();
if(fabs(nows.ans-24.0) < eps && nows.State == 15){
flag = 1;
output(nows);
break;
}
for (int i = 0; i < 4; i++)
{
if((nows.State & (1<<i)) == 0)
{
next.State = nows.State | (1<<i);
for (int j = 0; j < 6; j++)
{
if(j==0)next.ans = nows.ans + a[i];
else if(j==1)next.ans = nows.ans - a[i];
else if(j==2)next.ans = a[i] - nows.ans;
else if(j==3)next.ans = a[i] * nows.ans;
else if(j==4 && nows.ans!=0)next.ans = a[i]/nows.ans;
else if(j==5)next.ans = nows.ans/a[i];
else continue;
next.p = nows.p + 1;
for (int k = 0; k < next.p; k++){
next.op[k] = nows.op[k];
next.ch[k] = nows.ch[k];
}
next.ch[next.p] = a[i];
next.op[next.p] = j;
Q.push(next);
}
}
}
Q.pop();
}
if(flag) /*printf("yes\n")*/;
else{
//多1种情况 :某两个操作得到一个结果,另两个操作得到一个结果,二者组合得到一个结果。
//如 12,12,12,10
if(check(a[0],a[1],a[2],a[3])||check(a[0],a[2],a[1],a[3])||check(a[0],a[3],a[1],a[2]))flag = 1;
if(flag)/*printf("yes\n")*/;
else printf("no\n");
}
}
return 0;
}
一些数据:
2 5 5 10
3 3 7 7
4 4 7 7
3 7 9 13
6 9 9 10
12 12 12 10
9 11 12 12