永发信息网

用lua编程,三道题目

答案:1  悬赏:40  手机版
解决时间 2021-03-24 11:10
  • 提问者网友:爱唱彩虹
  • 2021-03-23 14:20
用lua编程,三道题目
最佳答案
  • 五星知识达人网友:大漠
  • 2021-03-23 15:03
Test1

local a[10]={3,4,6,7,8,4,2,1,9,10}

function q1(array)
local swap = 0

local newArray = {}
for i = 1, #array do

for j = i + 1, #array do

if array[j] < array[i] then
swap = array[i]

array[i] = array[j]

array[j] = swap

end
end
newArray[1] = array[1]

for i = 1, #array do

if array[i] != newArray[#newArray] then

newArray[#newArray + 1] = array[i]

end

end
return newArray

end

function q2(array)
local array1 = {}

local array2 = {}

for i = 1, #array do

if array[i] % 2 == 0 then

array2[#array2 + 1] = array[i]

else

array1[#array1 + 1] = array[i]

end

end

return array1, array2

end追问后面的呢?救我
不行T_T追答Test 2

local a = "Hello world!!"

function toCharArray(str)

local charArray = {}
for i = 1, #str do
charArray[#charArray + 1] = string.sub(str, i, i)
end

return charArray
end

function count(charArray)

local numArray = {}
for i = 1, #charArray do
if numArray[charArray[i]] then
numArray[charArray[i]] = numArray[charArray[i]] + 1
else
numArray[charArray[i]] = 1
end
end

return numArray
end

function main(str)
if #str > 50 then
return false
end

str = string.lower(str)
local charArray = toCharArray(str)
local numArray = count(charArray)

for k,v in pairs(numArray) do
if 97 <= string.byte(k, 1) and string.byte(k, 1) <= 122 then
print(k, v)
end
end
end

main(a)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯