
#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
}
在网上找了个代码,我改了下,符合你的三个要求了。#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")
}
算24点的技巧:有基本算式法,特性求解法,倍数法,巧用分数法,具体解法如下:1、基本算式法
利用2*12=24,3*8=24,4*6=24求解。一般情况下,先要看四张牌中是否有2,3,4,6,8,Q,如果有,考虑用乘法,将剩余的三个数凑成对应数。如3,3,6,10可组成(10-6/3)*3=24。如果没有2,3,4,6,8,Q,看是否能先把两个数凑成其中之一,再求解24。
2、特性求解法
1)利用0、11的运算特性求解。如(3,4,4,8)可组成3*8+4-4=24。
2)如果有两个相同的6,剩下的只要能凑成2,3,4,5都能算出24,比如6,6,3可以3*6+6=24。同理,如果有两个相同的8,剩下的只要能凑成2,3,4就能算出24。
如(2,5,8,8),(5-2)*8=24,多一个8,可以用乘法的分配律消去8,将算式改为5*8-2*8,将多余的8消去;如果有两个相同的Q,剩下的只要能凑成1,2,3就能算出24,如(9,J,Q,Q)可以12*11-12*9=24。
3、倍数法
利用24的倍数求解2*24=48,3*24=72,4*24=96,5*24=120,6*24=144想办法去凑48,72,96,120,144来求解。在具体的运算过程中,先将数乘得很大,最后再除以一个数得24。
4、巧用分数法
利用24的分数求解。先将数算成分数或小数,最后乘以一个数得24。用一个数除以一个分数,相当于乘以这个数的倒数,最后得24。
24点的口诀为见3凑8,见4凑6,见2凑12等等。
稍微特殊点的规律:2乘10+4,15+9,21+3,14+10等。
扩展资料:
“算24点”作为一种扑克牌智力游戏,还应注意计算中的技巧问题。计算时,我们不可能把牌面上的4个数的不同组合形式——去试,更不能瞎碰乱凑。这里向大家介绍几种常用的、便于学习掌握的方法:
1.利用3×8=24、4×6=24求解。
把牌面上的四个数想办法凑成3和8、4和6,再相乘求解。如3、3、6、10可组成(10—6÷3)×3=24等。又如2、3、3、7可组成(7+3—2)×3=24等。实践证明,这种方法是利用率最大、命中率最高的一种方法。
2.利用0、11的运算特性求解。
如3、4、4、8可组成3×8+4—4=24等。又如4、5、J、K可组成11×(5—4)+13=24等。
3.在有解的牌组中,用得最为广泛的是以下六种解法:(我们用a、b、c、d表示牌面上的四个数)
①(a—b)×(c+d)
如(10—4)×(2+2)=24等。
②(a+b)÷c×d
如(10+2)÷2×4=24等。
③(a-b÷c)×d
如(3—2÷2)×12=24等。
④(a+b-c)×d
如(9+5—2)×2=24等。
⑤a×b+c—d
如11×3+l—10=24等。
⑥(a-b)×c+d
如(4—l)×6+6=24等。
游戏时,同学们不妨按照上述方法试一试。
需要说明的是:经计算机准确计算,一副牌(52张)中,任意抽取4张可有1820种不同组合,其中有458个牌组算不出24点,如A、A、A、5。
经典24点
4
4
10
10
这个难点在于先要算出一个很大的数,就是100,然后再通过减4,除4就可以得到24点:(10×10-4)÷4=24。
6
9
9
10
这个也要先算大数90,然后除6,再加9即可得24点:9×10÷6+9=24。
2
2
2
9
这个并不难,只是数字比较好玩,包含了3个2,其计算方法为:(2+9)×2+2=24。
1、概述
给定4个整数,其中每个数字只能使用一次;任意使用 + - * / ( ) ,构造出一个表达式,使得最终结果为24,这就是常见的算24点的游戏。这方面的程序很多,一般都是穷举求解。本文介绍一种典型的算24点的程序算法,并给出两个具体的算24点的程序:一个是面向过程的C实现,一个是面向对象的java实现。
2、基本原理
基本原理是穷举4个整数所有可能的表达式,然后对表达式求值。
表达式的定义: expression = (expression|number) operator (expression|number)
因为能使用的4种运算符 + - * / 都是2元运算符,所以本文中只考虑2元运算符。2元运算符接收两个参数,输出计算结果,输出的结果参与后续的计算。
由上所述,构造所有可能的表达式的算法如下:
(1) 将4个整数放入数组中
(2) 在数组中取两个数字的排列,共有 P(4,2) 种排列。对每一个排列,
(2.1) 对 + - * / 每一个运算符,
(2.1.1) 根据此排列的两个数字和运算符,计算结果
(2.1.2) 改表数组:将此排列的两个数字从数组中去除掉,将 2.1.1 计算的结果放入数组中
(2.1.3) 对新的数组,重复步骤 2
(2.1.4) 恢复数组:将此排列的两个数字加入数组中,将 2.1.1 计算的结果从数组中去除掉
可见这是一个递归过程。步骤 2 就是递归函数。当数组中只剩下一个数字的时候,这就是表达式的最终结果,此时递归结束。
在程序中,一定要注意递归的现场保护和恢复,也就是递归调用之前与之后,现场状态应该保持一致。在上述算法中,递归现场就是指数组,2.1.2 改变数组以进行下一层递归调用,2.1.3 则恢复数组,以确保当前递归调用获得下一个正确的排列。
括号 () 的作用只是改变运算符的优先级,也就是运算符的计算顺序。所以在以上算法中,无需考虑括号。括号只是在输出时需加以考虑。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)