#include<stdio.h>
double
fun(double
a1,double
a2,int
b)
{switch(b)
{case
0:return
(a1+a2)
case
1:return
(a1-a2)
case
2:return
(a1*a2)
case
3:return
(a1/a2)
}
}
void
main()
{int
i,j,k,l,n,m,r,save[4],flg=1
double
num[4]={1,1,1,1},tem1,tem2,tem3,abc=1111
char
sign[5]="+-*/"
printf("输入四个告液数:")
for(i=0i<4i++)
{scanf("%lf",num+i)
save[i]=num[i]if(save[i]>13)flg=0}
if(flg)
{
flg=0
for(i=0i<4i++)
for(j=0j<4j++)
if(j!=i)
{for(k=0k<4k++)
if(k!=i&&k!=j)
{for(l=0l<4l++)
if(l!=i&&l!=j&&l!=k)
{for(n=0n<4n++)
for(m=0m<4m++)
for(r=0r<4r++)
{tem1=fun(num[i],num[j],n)
tem2=fun(tem1,num[k],m)
tem3=fun(tem2,num[l],r)
if(tem3==24.0)
{printf("{(%d%c%d)%c%d}%c%d=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l])return}
else
if(tem3==-24.0)
{printf("{%d%c(%d%c%d)}%c%d=24\n",save[k],sign[m],save[i],sign[n],save[j],sign[r],save[l])return}
else
if(tem3==1.0/24.0)
{printf("%d%c{(%d%c%d)%c%d}=24\n",save[l],sign[r],save[i],sign[n],save[j],sign[m],save[k])return}
else
if(tem3==-1.0/24.0)
{printf("%d%c{%d%c(%d%c%d)}=24\n",save[l],sign[r],save[k],sign[n],save[i],sign[m],save[j])return}
else
{tem1=fun(num[i],num[j],n)
tem2=fun(num[k],num[l],r)
tem3=fun(tem1,tem2,m)
if(tem3==24.0)
{printf("(%d%c%d)%c(%d%c%d)=24\n",save[i],sign[n],save[j],sign[m],save[k],sign[r],save[l])return}
}
}
}
}
}
}
if(!flg)
printf("NO
ANSWER\n")
}
#ifndef _24_H#define _24_H
//随机取4张牌
void GivePuzzle(char* buf)
//洗牌
void shuffle(char * buf)
//纸牌数值转换
int GetCardValue(int c)
//获取 *** 作符
char GetOper(int n)
//对表达式求值
double MyCalcu(double op1, double op2, int oper)
//输出
void MakeAnswer(char* answer, int type, char* question, int* oper)
//解题
int TestResolve(char* question, int* oper, char* answer)
int TryResolve(char* question, char* answer)
#endif
#include"24.h"
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<time.h>
//随机取4张牌
void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}
int i
for(i=0 i<4 i++){
buf[i] = card[rand()%13]
}
}
//洗牌
void shuffle(char * buf)
{
int i
char t
for(i=0 i<5 i++)
{
int k = rand()%4
t = buf[k]
buf[k] = buf[0]
buf[0] = t
}
}
//纸牌数值转换
int GetCardValue(int c)
{
if(c=='T') return 10
if(c>='0' && c<='9') return c - '0'
return 1
}
//获取 *** 作符
char GetOper(int n)
{
switch(n)
{
case 0:
return '+'
case 1:
宽铅 return '-'
case 2:
滚巧歼 return '大冲*'
case 3:
return '/'
}
return ' '
}
//对表达式求值
double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2
case 1:
return op1 - op2
case 2:
return op1 * op2
case 3:
if(fabs(op2)>0.0001)
return op1/op2
else
return 100000
}
return 0
}
//输出
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3]
int i
for(i=0 i<4 i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10")
else
sprintf(p[i], "%c", question[i])
}
switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])
break//A*(B*(c*D))
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])
break//A*((B*C)*D)
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])
break//(A*B)*(C*D)
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])
break//((A*B)*C)*D
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3])
break//(A*(B*C))*D
}
}
//解题
int TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
//question=char buf1[4]
//answer=char buf2[30]
double temp,temp1
int a=0
temp=MyCalcu(GetCardValue(question[2]),GetCardValue(question[3]),oper[2])
temp=MyCalcu(GetCardValue(question[1]),temp,oper[1])
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0])
if(temp==24)
{
MakeAnswer(answer,0,question,oper)
a=1
}
temp=MyCalcu(GetCardValue(question[1]),GetCardValue(question[2]),oper[1])
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2])
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0])
if(temp==24l)
{
MakeAnswer(answer,1,question,oper)
a=1
}
temp=MyCalcu(GetCardValue(question[0]),GetCardValue(question[1]),oper[0])
temp1=MyCalcu(GetCardValue(question[2]),GetCardValue(question[3]),oper[2])
temp=MyCalcu(temp,temp1,oper[1])
if(temp==24l)
{
MakeAnswer(answer,2,question,oper)
a=1
}
temp=MyCalcu(GetCardValue(question[0]),GetCardValue(question[1]),oper[0])
temp=MyCalcu(temp,GetCardValue(question[2]),oper[1])
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2])
if(temp==24l)
{
MakeAnswer(answer,3,question,oper)
a=1
}
temp=MyCalcu(GetCardValue(question[1]),GetCardValue(question[2]),oper[1])
temp=MyCalcu(GetCardValue(question[0]),temp,oper[0])
temp=MyCalcu(temp,GetCardValue(question[3]),oper[2])
if(temp==24l)
{
MakeAnswer(answer,4,question,oper)
a=1
}
return a
}
//解题
int TryResolve(char* question, char* answer)
{
int i,j
int oper[3] // 存储运算符,0:加法 1:减法 2:乘法 3:除法
for(i=0 i<1000 * 1000 i++)
{
// 打乱纸牌顺序
shuffle(question)
// 随机产生运算符
for(j=0 j<3 j++)
oper[j] = rand() % 4
if(TestResolve(question, oper, answer))
return 1
}
return 0
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<time.h>
int main()
{
int j
char buf1[4] // 题目
char buf2[30],ch // 解答
// 初始化随机种子
srand( (unsigned)time( NULL ) )
printf("***************************\n")
printf("计算24\n")
printf("A J Q K 均按1计算,其它按牌点计算\n")
printf("目标是:通过四则运算组合出结果:24\n")
printf("***************************\n\n")
for()
{
GivePuzzle(buf1) // 出题
printf("题目:")
for(j=0 j<4 j++)
{
if( buf1[j] == 'T' )//、、、、初始化buf1[]
printf(" 10")
else
printf("%6c", buf1[j])
}
printf("\n")
system("pause")
if(TryResolve(buf1, buf2)) // 解题
{
printf("参考:%s\n", buf2)
}
else
printf("可能是无解…\n")
printf("按任意键出下一题目,x 键退出…\n")
ch=getchar()
if(ch== 'x'||ch=='X' )
break
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)