速算24点检测生成核心lua

屏幕上输入1〜10范围内的4个整数(可以有重复),对它们进行加、减、乘、除四则运算后(可以任意的加括号限定计算的优先级),寻找计算结果等于24的表达式。\n\n例如输入4个整数4、5、6、7,可得到表达式:4*((5-6)+7)=24。这只是一个解,要求输出全部的解。要求表达式中数字的顺序不能改变。\n\n思路:\n拼凑加减乘除,使4个数的运算结果等于24。\n\n由于四则运算中,乘除的优先级高于加减,所以必须"加括号"来限定4个数之间运算优先级。\n\n例如:A+B*C-D 这个式子,通过增加括号,可以产生多种结果,比如 (A+B)*(C-D) 和 A+(B*C-D)。\n\n那么总共有几种加括号的方法呢,该如何分类呢?\n\n要想将4个数字的运算优先级细分,必须使用两对括号。\n\n可以这么理解:我们的目的是将4个数的运算转换成两个"数"的运算(这里的"数"包括括号表达式),而每两个数运算,就能得出一个结果,即每对括号可以减少一个要计算的数字(如(A+B)*(C+D)中,A和B运算,使式子变成了3个数,C接着和D运算,使式子剩下两个数字)。4-2=2即为需要的括号数。\n\n下面列举所有可能的括号表达式:(#表示四则运算符)\n\n((A#B)#C)#D\n(A#(B#C))#D\nA#((B#C)#D)\nA#(B#(C#D))\n(A#B)#(C#D)\n具体思路:\n\n上面5种括号表达式都可以单独写成函数,函数内部按照括号的优先级+从左往右的顺序进行运算,最后返回计算结果。\n\n每个表达式中有3个'#'号,它们是四则运算符(+、-、*、/),可以定义一个全局字符数组,存放4这四个字符。\n\nchar my_oprator[4] = {'+', '-', '*', '/'};\n

--24点游戏制作

--星空露珠工作室出品

local flag=false

local operator={'+','-','*','/'}

local nr,nr0,nrnew={},{},{}

local tb1,tb0,t0,t1,t2={},{},{},{},{}

local str,str0="",""

--运算两个数返回运算值

local function ce(nr1,nr2,operator0)

--print(nr1,nr2,operator0)

if operator0=='+' then return nr1+nr2

elseif operator0=='-' then return nr1-nr2

elseif operator0=='*' then return nr1*nr2

elseif operator0=='/' and nr2~=0 then return nr1/nr2

else return -1

end

end

--获取重复数组个数

local function c(t)

local t0={}

local t1={}

local t2={}

for _,v in ipairs(t)

if t0[v]==nil then t1[#t1+1]=v t0[v]=1 else t0[v]=t0[v]+1 end

end

if #t1==1 then t2={t1[1],t1[1],t1[1],t1[1]}

elseif #t1==3 then

if t0[t1[1]]==2 then t2={t1[2],t1[3],t1[1],t1[1]}

elseif t0[t1[2]]==2 then t2={t1[1],t1[3],t1[2],t1[2]}

elseif t0[t1[3]]==2 then t2={t1[1],t1[2],t1[3],t1[3]}

end

elseif #t1==2 then

if t0[t1[1]]==2 then t2={t1[1],t1[1],t1[2],t1[2]}

elseif t0[t1[1]]==1 then t2={t1[1],t1[2],t1[2],t1[2]}

elseif t0[t1[1]]==3 then t2={t1[2],t1[1],t1[1],t1[1]}

end

elseif #t1==4 then t2={t1[1],t1[2],t1[3],t1[4]}

end

return t1,t0,t2

end

--进行运算组合

local function c2(num1,num2,num3,num4)

for i=1,4 do

operator1=operator[i]

firstResult =ce(num1,num2,operator1)

midResult =ce(num2,num3,operator1)

tailResult =ce(num3,num4,operator1)

for j=1,4 do

operator2=operator[j]

firstMidResult = ce(firstResult,num3,operator2)

firstTailResult= ce(num3,num4,operator2)

midFirstResult = ce(num1,midResult,operator2)

midTailResult = ce(midResult,num4,operator2)

tailMidResult = ce(num2,tailResult,operator2)

for k=1,4 do

operator3=operator[k]

if ce(firstMidResult,num4,operator3)==24 then

str0=str0.."(("..num1..operator1..num2..")"..operator2..num3..")"..operator3..num4..','

flag = true return

end

if ce(firstResult,firstTailResult,operator3) == 24

str0=str0.."("..num1..operator1..num2..")"..operator3.."("..num3..operator2..num4..")"..','

flag = true return

end

if ce(midFirstResult,num4,operator3) == 24

str0=str0.."("..num1..operator2.."("..num2..operator1..num3.."))"..operator3..num4..','

flag = true return

end

if ce(num1,midTailResult,operator3) == 24

str0=str0..num1..operator3.."(("..num2..operator1..num3..")"..operator2..num4..")"..','

flag = true return

end

if ce(num1,tailMidResult,operator3) == 24

str0=str0..num1..operator3.."("..num2..operator2.."("..num3..operator1..num4.."))"..','

flag = true return

end

end

end

end

end

--各种排序组合

local function c1(nrnew)

local tb1,tb0={},{}

local nr={}

local tb1,tb0,nr=c(nrnew)

--print(unpack(tb1))

print(unpack(nrnew))

str0="key="

if #tb1==1 then c2(nr[1],nr[2],nr[3],nr[4])

elseif #tb1==3 then c2(nr[1],nr[2],nr[4],nr[4])

c2(nr[1],nr[4],nr[2],nr[4])

c2(nr[1],nr[4],nr[4],nr[2])

c2(nr[2],nr[1],nr[4],nr[4])

c2(nr[2],nr[4],nr[1],nr[4])

c2(nr[2],nr[4],nr[4],nr[1])

c2(nr[4],nr[4],nr[2],nr[1])

c2(nr[4],nr[2],nr[4],nr[1])

c2(nr[4],nr[4],nr[1],nr[2])

c2(nr[4],nr[1],nr[4],nr[2])

c2(nr[4],nr[1],nr[2],nr[4])

c2(nr[4],nr[2],nr[1],nr[4])

elseif #tb1==2 then

if math.abs(tb0[tb1[1]]-tb0[tb1[2]])==0 then

c2(nr[2],nr[2],nr[4],nr[4])

c2(nr[2],nr[4],nr[2],nr[4])

c2(nr[2],nr[4],nr[4],nr[2])

c2(nr[4],nr[2],nr[2],nr[4])

c2(nr[4],nr[2],nr[4],nr[2])

c2(nr[4],nr[4],nr[2],nr[2])

elseif math.abs(tb0[tb1[1]]-tb0[tb1[2]])==2 then

c2(nr[1],nr[4],nr[4],nr[4])

c2(nr[4],nr[1],nr[4],nr[4])

c2(nr[4],nr[4],nr[1],nr[4])

c2(nr[4],nr[4],nr[4],nr[1])

end

elseif #tb1==4 then

for i=1,4 do

for j=1,4 do

if i~=j then

for m=1,4 do

if m~=i and m~=j then

for n=1,4 do

if n~=i and n~=j and n~=m then

c2(nr[i],nr[j],nr[m],nr[n])

end

end

end

end

end

end

end

end

end

local game={}

--全部题目生成 也可不需要生成,生成时判断可否有解

local function main0()

local nr={}

for i=1,13 do

for j=i,13 do

for m=j,13 do

for n=m,13 do

flag = false

local nr={i,j,m,n}

c1(nr)

if flag==true then

table.insert(game,'{'..i..','..j..','..m..','..n..str0..',},')

--str=str..'{'..i..','..j..','..m..','..n..str0..'},'

end

end

end

end

end

end

--main0() --生成全部

c1({3,6,7,10}) --输入求解

print(str0)

相关推荐
老蒋每日coding2 小时前
Python3基础练习题详解,从入门到熟练的 50 个实例(一)
开发语言·python
历程里程碑2 小时前
Linux15 进程二
linux·运维·服务器·开发语言·数据结构·c++·笔记
lly2024062 小时前
网站主机提供商:如何选择最适合您的服务
开发语言
HAPPY酷2 小时前
构建即自由:一份为创造者设计的 Windows C++ 自动化构建指南
开发语言·c++·ide·windows·python·策略模式·visual studio
工一木子2 小时前
Java 的前世今生:从 Oak 到现代企业级语言
java·开发语言
中二病码农不会遇见C++学姐2 小时前
《文明6》Leaders.xml 文件标签解析指南
游戏
啟明起鸣2 小时前
【C++20新特性】概念约束特性与 “模板线程池”,概念约束是为了 “把握未知对象”
开发语言·c++·c++20·模板线程池
what丶k2 小时前
SpringBoot3 缓存抽象深度实践:Caffeine+Redis多级缓存,穿透/雪崩/击穿防御全方案
数据库·redis·缓存
咖啡の猫2 小时前
Redis简单介绍
数据库·redis·缓存