Lua 中的常用练习题(求表中的最大值,最小值。)

 求Lua表中的最大值最小值


local arr = {1,2,3,4,5,5,5,5,8,9,99,-10}
-- 定义数组
local max = math.max(unpack(arr))
-- math.max取得参数中最大值;
-- unpack接收一个数组(table)参数,默认从下标1开始返回数组所有元素

print(max)

function  MaxFunction(arr)

max1 = nul
	for k ,v  in pairs(arr) do

		if(max1==nul) then
			max1 = v
		end
		if max1 < v then
			max1=v
		end
	end
	return max1
end
print( MaxFunction(arr))

x数字的n次幂

function   __mi(x,n)
local  sum
	if n == 1 then
		sum=x
	else
		sum  = __mi(x,n-1) * x
	end

return sum

end

print(__mi(2,5))

N的阶乘

-- 方法1
function   __jiecheng(x)
local  i=1
	if x < 1 then
		return 1
	end
	repeat i = x * i
			x = x - 1
			until x == 0
return i
end
print(__jiecheng(4))
-- 方法2
function   _jiecheng(n)
local  sum
	if n == 1 then
		sum=1
	else
		sum  =_jiecheng(n-1) * n
	end
return sum
end
print(_jiecheng(4))

杨辉三角

a = 5
for i = 1, a, 1 do
	for j = 1, a - i, 1 do
		io.write(' ')
	end
	for k = 1, 2 * i - 1, 1 do
		io.write(k)
	end
	print()
end

99乘法表

for a = 1,9 do
    local s = ""
    for b=1,9 do
        if b <= a then
            s = s..a.."X"..b.."="..a*b
            if a ~= b then
                s = s.."\t"
            end
        end
    end
    print(s)
end

 要求用户输入一个年份,判断并输出该年份是闰年还是平年。

year = io.read()
if(year%4==0 and year%100~=0) then
print("闰年")
else
print("平年")
end

 要求用户输入两个整数,判断第一个整数是否是第二个整数的倍数。

num1 = io.read()
num2 = io.read()
if(num2%num1 == 0) then
print("Ok")
else
print("No")
end

要求用户输入一个年份和一个月份,判断(要求使用嵌套的if…else和switch分别判断一次)该年该月有多少天。

print("请输入年份")
year = io.read();
print("请输入月份")
month = io.read();
if(year%4 == 0 and year%100 ~= 0) then
	print(year.."是闰年:366天")
	local month = tonumber(month)
	if(month == 2) then
		print(month.."月是28天")
	end
else								   
	print(year.."是平年:365天")	  
	local month = tonumber(month)
	if(month == 2) then
		print(month.."月是29天")
	end
end
 
monthMax = {1,3,5,7,8,10,12}		
months = {4,6,9,11}					
for i,m in ipairs(monthMax)do		
	local lable = tonumber(month)	
	if m == lable then			
		print(lable.."月是31天")
	end
end
 
for i,m in ipairs(months)do		
	local lable = tonumber(month)
	if m == lable then
		print(lable.."月是30天")
	end
end

要求用户输入一个学生的分数(1~100),使用switch结构判断该分数属于什么等级(A、B、C、D、F)。

userScore = io.read()
numberScore = tonumber(userScore)
 
if (numberScore <= 20 and numberScore > 0) then
print("A")
end
if (numberScore <= 40 and numberScore > 20) then
print("B")
end
if (numberScore <= 60 and numberScore > 40) then
print("C")
end
if (numberScore <= 80 and numberScore > 60) then
print("D")
end
if (numberScore <= 100 and numberScore > 80) then
print("F")
end
if (numberScore <= 0) then
print("会好好输吗?")
end

使用while循环求1~100以内所有奇数的和。

i = 1;
J = 1;
while(i < 99)
do
	local num = i + 2
	i = num
	J = J + i
end
print(J)
--]]
 

使用while循环求式子2+22+222+2222+22222的和。p=p*10+2;

i = 0
j = 0
while(i < 22222)
do
	i = i * 10 + 2
	j = j + i
end
print(j)

请编程验证一下“角谷猜想”:对任意的自然数,若是奇数,就对它乘以3加1;若是偶数就对它除以2;这样得到一个新数,再按上述奇数、偶数的计算规则进行计算,一直进行下去,最终将得到1。

userScore = io.read()
numberScore = tonumber(userScore)
while(numberScore ~= 1) do
	if(numberScore % 2 == 1) then
		numberScore = numberScore * 3 + 1
	else
		numberScore = numberScore / 2
	end
	print(numberScore)
end

猜你喜欢

转载自blog.csdn.net/qq_36848370/article/details/103385175
今日推荐