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
原创粉丝点击