-- 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
相关推荐
GIS数据转换器4 小时前
地下空间综合管廊运营管理平台Wang's Blog9 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图砚底藏山河9 小时前
python量化:行情数据回溯今年下半年9 小时前
AI智能体项目实战记录(02):初始化 Python 项目Williamtym10 小时前
PickGo Lab实践:用浏览器、Python和MCP串起机器人搬运仿真DongQiShanRen10 小时前
六根硬件映射(四):归属澄清、两处不一致与卷十一总表CS创新实验室10 小时前
Python 对象“死“了之后去了哪?——引用计数与分代垃圾回收quantdash_cc10 小时前
Pandas 处理分钟级股票行情:从原始数据清洗到 K 线聚合yu_zhidun10 小时前
企业电脑监控软件怎么选:别只看功能清单,先看这 6 个底层能力无忧.芙桃11 小时前
C++语言原理与实践(十六):多态机制讲解