local t = {1, 7, 2, 5, 4, 9, 9, 7, 2, 2, 2, 5, 5} function seekNum(t) local dic_count = {} local dic_pos = {} for i=1,#t do if dic_count[tostring(t[i])] == nil then dic_count[tostring(t[i])] = 1 else local index = table.indexOf(t, t[i]) dic_count[tostring(t[i])] = dic_count[tostring(t[i])] + 1 local index = table.indexOf(t, t[i]) if index > 0 then if dic_pos[tostring(index)] == nil then dic_pos[tostring(index)] = t[i] end end dic_pos[i] = t[i] end end local tb_number = {} for number,count in pairs(dic_count) do if tonumber(count) ~= 1 then print("数字[ " .. number .. " ] 出现 [ " .. count .. " ]次") tb_number[#tb_number + 1] = number end end print("========================================") local T = {} for _,number in pairs(tb_number) do local _t = {} for pos,_number in pairs(dic_pos) do if number == tostring(_number) then table.insert(_t, pos) end end T[tostring(number)] = _t end for k,v in pairs(T) do print("数字[ ".. k .. " ]位置在") print("-------------------------") if type(v) == "table" then for _, _v in pairs(v) do print(_v) end end print("-------------------------") end end function table.indexOf(array, value) if array then local i, max = 1, #array while i <= max do if array[i] == value then return i end i = i + 1 end end return -1 end seekNum(t)
原文地址:https://www.cnblogs.com/lxyang0928/p/11140011.html
时间: 2024-10-11 13:14:19