写一个:定义一个不规则矩形,遍历二维数组,判断鞍点是否越界的lua代码
时间: 2024-03-07 16:47:41 浏览: 65
C语言程序设计-求一个二维数组中的鞍点
5星 · 资源好评率100%
以下是符合您要求的 Lua 代码:
```lua
-- 定义不规则矩形
local rectangle = {
{1, 2},
{3, 4},
{5, 6, 7},
{8, 9, 10},
{11}
}
-- 计算鞍点
local function findSaddlePoint(matrix)
local row = #matrix
local col = #matrix[1]
for i = 1, row do
for j = 1, col do
local value = matrix[i][j]
local isSaddlePoint = true
-- 判断是否为行最小值
for k = 1, col do
if matrix[i][k] < value then
isSaddlePoint = false
break
end
end
-- 判断是否为列最大值
if isSaddlePoint then
for k = 1, row do
if matrix[k][j] > value then
isSaddlePoint = false
break
end
end
end
-- 判断是否越界
if isSaddlePoint and (i > row or j > #matrix[i]) then
return false
end
end
end
return true
end
-- 遍历二维数组,判断鞍点是否越界
for i = 1, #rectangle do
local row = rectangle[i]
for j = 1, #row do
local element = row[j]
if findSaddlePoint(rectangle) then
print("Saddle point found at (" .. i .. ", " .. j .. ")")
else
print("No saddle point found at (" .. i .. ", " .. j .. ")")
end
end
end
```
其中 `rectangle` 是一个不规则矩形,`findSaddlePoint` 函数用于计算矩阵中的鞍点,并判断是否越界。最后遍历矩阵中的每个元素,调用 `findSaddlePoint` 函数判断是否存在鞍点,并输出结果。
阅读全文