-- 1. 表操作
-- 给定表:
-- t = {10, 20, 30, 40, 50}
-- 写一个函数 sum(tbl) 计算表中所有元素的和。
Lualocal function sum(tbl) local total = 0 for _, v in ipairs(tbl) do if type(v) == "number" then total = total + v end end return total end local t = {10, 20, 30, 40, 50} print(sum(t))
-- 2. 字符串处理
-- 写一个函数,输入一个字符串 "hello world",输出反转后的结果 "dlrow olleh"。
Lualocal function reverseStr(str) return string.reverse(str) end local s = "hello world" print(reverseStr(s))
-- 3. 条件判断
-- 写一个函数 isEven(n) 判断一个整数是否为偶数,返回 true 或 false。
Lualocal function isEven(n) if type(n) ~= "number" then print("请输入数字") return end return n % 2 == 0 end print(isEven(24))
-- 4. 频率统计
-- 给定表:
-- fruits = {"apple", "banana", "apple", "orange", "banana", "apple"}
-- 写一个函数 countFreq(tbl) 返回每个元素出现的次数,例如:
-- { apple = 3, banana = 2, orange = 1 }
Lualocal function countFreq(tbl) local freq = {} for _, v in ipairs(tbl) do freq[v] = (freq[v] or 0) + 1 end return freq end local fruits = {"apple", "banana", "apple", "orange", "banana", "apple"} local nT = countFreq(fruits) for index, value in pairs(nT) do print(index .. value) end
-- 5. 闭包应用
-- 写一个函数 counter(),返回一个闭包函数,每次调用时返回一个递增的数字:
Lualocal c = counter() print(c()) --> 1 print(c()) --> 2 print(c()) --> 3 local function counter() local currentCount = 0 return function() currentCount = currentCount + 1 return currentCount end end local c = counter() print(c()) print(c()) print(c())
-- 6. 冒泡排序
-- 给定一个数字表 {5, 1, 4, 2, 8},写一个函数用冒泡排序将其从小到大排列。
Lualocal function bubbleSort(t) local n = #t for i = 1, n - 1 do for j = 1, n - i do if t[j] > t[j+1] then t[j], t[j+1] = t[j+1], t[j] end end end return t end local o = {5, 1, 4, 2, 8} local newT = bubbleSort(o) for index, value in ipairs(newT) do print(index .. " " .. value) end
-- 7. 迭代器
-- 写一个自定义迭代器 evenIterator(tbl),只遍历表中的偶数。
Luafor v in evenIterator({1,2,3,4,5,6}) do print(v) -- 输出 2, 4, 6 end local function evenIterator(tbl) local i = 0 return function() i = i + 1 while tbl[i] do if tbl[i] % 2 == 0 then return tbl[i] end i = i + 1 end end end for v in evenIterator({1,2,3,4,5,6}) do print(v) -- 2, 4, 6 end for v in evenIterator({1,2,3,4,5,6}) do print(v) -- 输出 2, 4, 6 end
-- 8. 元表应用
-- 定义一个表 Vector,支持向量加法:
-- v1 = Vector.new(1, 2)
-- v2 = Vector.new(3, 4)
-- v3 = v1 + v2 -- 结果应为 (4, 6)
Lualocal Vector = {} Vector.__index = Vector function Vector.__tostring(v) return "(" .. v.x .. ", " .. v.y .. ")" end Vector.__add = function (v1, v2) local obj = {} obj = Vector:new(0, 0) if type(v1.x) == "number" and type(v2.x) == "number" and type(v1.y) == "number" and type(v2.y) == "number" then obj.x = v1.x + v2.x obj.y = v1.y + v2.y end return obj end function Vector.new(x, y) local obj = {} obj.x = type(x) == "number" and x or 0 obj.y = type(y) == "number" and y or 0 setmetatable(obj, Vector) return obj end local v1 = Vector.new(1, 2) local v2 = Vector.new(3, 4) local v3 = v1 + v2 -- 结果应为 (4, 6) print(v3.x .. " " .. v3.y)
-- 9.协程
-- 写一个协程函数,每次调用只输出一句话:
Lualocal co = myCoroutine() coroutine.resume(co) -- 输出 "Step 1" coroutine.resume(co) -- 输出 "Step 2" coroutine.resume(co) -- 输出 "Step 3" local count = 1 local co = coroutine.create(function() while true do print("Step " .. tostring(count)) count = count + 1 coroutine.yield() end end) coroutine.resume(co) -- 输出 "Step 1" coroutine.resume(co) -- 输出 "Step 2" coroutine.resume(co) -- 输出 "Step 3" coroutine.resume(co) -- 输出 "Step 4"
【Lua】题目小练12
大飞pkz2025-08-29 16:29