--判断字符在字符串中出现的次数
local str = "you you hao bye you good"
local i = 0
for s in string.gmatch(str, "you") do
i=i+1
end
print(i) --3
--判断表中的数是否连续,0可以代表任意数!
local testTbl = {0,0,0,0,0,1,3,5,9}
function checkNum(testTbl)
--统计0的个数
local zeroNum = 0
for i = #testTbl, 1, -1 do
if testTbl[i] == 0 then
zeroNum = zeroNum + 1
table.remove(testTbl, i)
end
end
--排序
table.sort(testTbl, function (a, b)
return a < b
end )
--统计需要补0的个数
local needNum, maxNum = 0, testTbl[#testTbl]
for i=#testTbl-1, 1, -1 do
if maxNum == testTbl[i] then
return false --有相等的必不连续
end
local inteval = maxNum - testTbl[i] - 1
needNum = needNum + inteval
maxNum = testTbl[i]
end
return needNum <= zeroNum
end
print(checkNum(testTbl)) --true
--冒泡排序
local testTbl = {9,6,8,5,2,4,7,3,1}
for i=1,#testTbl do
for j=1,#testTbl - i do
if
Lua 练习题
于 2022-04-27 18:12:15 首次发布