一个写的24点,可是输出符号,有兴趣的大神帮看看一下呗,实在谢谢了
程序有点长,拜托各位耐心看看啦#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
int p[24][4] = {0};
int s = 0;
int data[4] = {0,1,2,3};
char dataopt[4] = {'+','-','*','/'};
void dfs(int mem[],bool foot[],int depth)
{
if(4 == depth)
{
int i = 0;
for(i = 0;i<4;i++)
p[s][i] = mem[i];
s++;return ;
}
int i;
for(i = 0;i<4;i++)
{
if(!foot[i])
{
foot[i] = true;
mem[depth] = data[i];
dfs(mem,foot,depth+1);
foot[i] = false;
}
}
}
int fun(int a,int b,char o)
{
switch(o)
{
case '+':
return a+b;
break;
case '-':
return a-b;
break;
case '*':
return a*b;
break;
case '/':
return a%b?-100000:a/b;
break;
}
}
//1+2-4*3
int compute(int a[],char b[])
{
bool h[50];
h[42] = true;h[43] = false;
h[45] = false;h[47] = true;
int s1[10];
char s2[10];
int i = 0,j = 0,k = 0;
int pa = 0,pb = 0;
s1[pa++] = a[i++];
s2[pb++] = b[j++];
while(pb)
{
if(0 == k && i<4)
{
s1[pa++] = a[i++];
k+=1;k%=2;
}
else
{
char temp = s2[pb-1];
if(j<3 && h[b[j]]>h[temp])
s2[pb++] = b[j++];
else
{
int _a = s1[pa-1];
pa--;
int _b = s1[pa-1];
pa--;
char _o = s2[pb-1];
pb--;
s1[pa++] = fun(_b,_a,_o);
if(j<3)
s2[pb++] = b[j++];
}
k+=1;k%=2;
}
}
return s1[pa-1];
}
int main()
{
int i,j,k,t,m,n;
scanf("%d",&n);
getchar();
bool foot[4] = {0};
int mem[4] = {0};
dfs(mem,foot,0);
while(n--)
{
int d[4];
scanf("%d%d%d%d",d,d+1,d+2,d+3);
for(i = 0;i<24;i++)
{
int _temp[4] = {d[p[i][0]],d[p[i][1]],d[p[i][2]],d[p[i][3]]};
for(j = 0;j<4;j++)
{
for(k = 0;k<4;k++)
{
for(t = 0;t<4;t++)
{
char temp[3] = {dataopt[j],dataopt[k],dataopt[t]};
if(24 == compute(_temp,temp))
{
printf("Yes.\n");
break;
}
}
if(t!=4)
break;
}
if(k!=4)
break;
}
if(j!=4)
break;
}
if(i == 24)
printf("No.\n");
}
return 0;
}