lua关于表的处理和输出方式整理
来源:互联网 发布:snmp数据采集 编辑:程序博客网 时间:2024/05/21 10:35
题目在文件里面,注意对表中元素操作时,不要改变原表元素。
若对t表元素操作
另取表t2=t,对t2中元素赋值,print(t)时 t中相应位置的值也改变了。
local x=0
local g = {}
for i,v in ipairs(t) do
table.insert(g,v);
end
采用此方法对t2表定义。
--直接用t2=t1,对t2做改变时t1中元素也会改变。
--一个不确定数据数目的表,0可以代表一个范围内的任意数,求输出的是不是等差数列
function istableorder(t,a,b)
local x=0
local g = {}
for i,v in ipairs(t) do
table.insert(g,v);
end
c={}
d={}
for i,v in pairs(t) do
if v~=0 then
x=x+1
table.insert(c,#c+1,i) --记录不是零的数的位置
table.insert(d,#d+1,v) --记录不是零的数的值
end
end
-- print(unpack(d))
-- print(unpack(c))
if x==0 then --全都为零
if(#t<=(b-a+1)) then
return true
elsereturn false
end
elseif x==1 then --有一个不是零
if c[1]==1 or c[1]==#t then
if ((d[1]+#t-1)>(b)) then
return false
elseif((d[1]-#t+1)<a) then
return false
end
return true
elseif(d[1]+(#t-c[1])<=b)and(d[1]-(c[1]-1)>=a) then --最大值小于零的范围
return true --最小值大于零的范围
elseif(d[1]-(#t-c[1])>=a)and(d[1]+(c[1]-1)<=b) then
return true
else
return false
end
else
local e
e=(d[2]-d[1])/(c[2]-c[1])
if c[1]==1 then
for i=1,#t-1 do
if g[i+1]~=g[i]+e then
if g[i+1]~=0 then
return false
else
g[i+1]=g[i]+e
if g[i+1]>b or g[i+1]<a then
return false
end
end
end
end
return true
elseif c[2]==#t then
for i=#t,2,-1 do
if g[i-1]~=g[i]-e then
if g[i-1]~=0 then
return false
else
g[i-1]=g[i]-e
if g[i-1]>b or g[i-1]<a then
return false
end
end
end
end
return true
-- else
-- for i=c[1],2,-1 do
-- if g[i-1]~=g[i]-e then
-- if g[i-1]~=0 then
-- return false
-- else
-- g[i-1]=g[i]-e
-- if g[i-1]>b or g[i-1]<a then
-- return false
-- end
-- end
-- end
-- end
-- for i=c[1],#t-1 do
-- if g[i+1]~=g[i]+e then
-- if g[i+1]~=0 then
-- return false
-- else
-- g[i+1]=g[i]+e
-- if g[i+1]>b or g[i+1]<a then
-- return false
-- end
-- end
-- end
-- end
-- return true --]]
end
end
end
-- a=6
-- b=7
-- t={5,0,0}
-- r={0,1,0,0,0,9}
-- s={0,0,0,0,5,0,0}
-- u={0,0,0,1,0,0,0,}
-- v={0,0,1,0,3}
-- w={0,0,3,0,1}
-- x={-1,0,0,1,0,3}
-- y={-1,0,1,2,0,0,5}
-- z={-2,0,0,0,0,0,0,}
-- sr={0,0,0,4,0,0,0,0,-1}
-- function shuchu( t )
-- print(unpack(t))
-- end
-- print(shuchu(t),istableorder(t,a,b))
-- print(shuchu(r),istableorder(r,a,b))
-- print(shuchu(s),istableorder(s,a,b))
-- print(shuchu(u),istableorder(u,a,b))
-- print(shuchu(v),istableorder(v,a,b))
-- print(shuchu(w),istableorder(w,a,b))
-- print(shuchu(x),istableorder(x,a,b))
-- print(shuchu(y),istableorder(y,a,b))
-- print(shuchu(z),istableorder(z,a,b))
-- print(shuchu(sr),istableorder(sr,a,b))
slot={
{1,2,3,4,5,6,7,8,0,0,0,0,0,0,0,0},
{1,2,3,4,5,6,7,7},
{-3,-1,1,3,5},
{-3,-1,1,3,4},
{1,0,2,5},
{1,0,3,4},
{0,0,0,1,3,5},
{0,0,1,2,0,4,5},
{0,0,1,2,0,0,4},
{0,0,1,2,0,0,5},
{0,0,0,1,0,0,0},
{0,0,0,0,0},
{1,5,6},
{1,5,9},
{5,4,3,2,1},
{2,0,0},
{0,0,0,0,0},
{1,0,0,0,0},
{0,0,0,0,1},
{0,0,1,0,0},
{0,1,0,3,0},
{1,2,0,0,0},
{0,0,0,1,2},
{1,2,3,4,5},
{2,4,6,8,10},
{3,5,7,0,11},
{-9,-7,0,0,-1},
{-9,0,0,0,-1},
{0,-7,-5,0,-1},
{0,0,0,-3,0},
{-9,0,0,0,0},
{0,0,0,0,-1},
{-9,0,-5,0,-1},
{0,-9,-7,0,0},
}
a=-1
b=3
for i=1,table.getn(slot) do
temp = slot[i];
if(istableorder(temp,a,b)) then
--print("ture|"..table.concat(temp,",").."}".." is order");
print("true".." "..table.concat(temp,',').." a="..a.." b="..b)
else
--print("false|"..table.concat(temp,",").."}".." is not order");
print("false".." "..table.concat(temp,',').." a="..a.." b="..b)
end
end
test={0,0,0,1,2}
istableorder(test,a,b)
print(unpack(test))
0 0
- lua关于表的处理和输出方式整理
- Lua 关于类的理解整理
- 关于数据库 System lock 状态的说明和处理方式
- python中三种处理文件输出的方式
- 关于异常的处理方式
- (整理)关于C语言中的输入和输出函数的一些运用
- lua递归输出表table的内容
- c++ 关于字符串处理函数的整理
- 百练+打印月历+日期的计算和格式化输出方式,细节注意处理额
- Mapreduce 处理gbk文件的方式(输入gbk文件和输出gbk文件)
- mini2440串口的输入和输出整理
- 关于python中文输出乱码的处理
- 关于redis的pipline和lua脚本
- 关于丢失表空间数据文件的处理方式
- Lua编程(三) 稀疏表、双端队列、格式化输出、表和循环表的格式化输出
- uboot2010.3关于LCD输出的处理-字符处理
- Lua中遍历表的方式
- 关于iOS的多线程处理方式
- 【Android 应用开发】GitHub 优秀的 Android 开源项目
- Bootstrap 简介
- 【Spring MVC】 学习笔记
- iOS 开发之内购 – AppStore
- 2016CCPC杭州站打铁纪念
- lua关于表的处理和输出方式整理
- RSA的密钥把JAVA格式转换成C#的格式(一)
- 两种socket获取本机IP地址方法及获取ip对应网卡
- QRCode - 二维码识别与生成
- 11G GRID 安装缺少到libcap.so.1而终止的解决方法
- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
- RSA的密钥把JAVA格式转换成C#的格式(二)
- python中几个内建函数
- Java基础:反射