Last active
August 29, 2015 14:05
-
-
Save hanxi/289142e94836e84c39b6 to your computer and use it in GitHub Desktop.
a+b=c a,b,c都是三位数且不重复
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local tostring = tostring | |
local tonumber = tonumber | |
local ssub = string.sub | |
local sformat = string.format | |
local tconcat = table.concat | |
local tinsert = table.insert | |
local si={1,2,3,4,5,6,7,8,9} | |
function printsi(si) | |
print(tconcat(si)) | |
end | |
local st = {} | |
function permutation(si,k,m) | |
local i,j | |
if k==m then | |
local tmp = {si[1],si[2],si[3],si[4],si[5],si[6],si[7],si[8],si[9]} | |
tinsert(st,tmp) | |
else | |
for j=k,m do | |
si[j],si[k] = si[k],si[j] | |
permutation(si,k+1,m) | |
si[j],si[k] = si[k],si[j] | |
end | |
end | |
end | |
permutation(si,1,9) | |
--for _,si in pairs(st) do | |
-- printsi(si) | |
--end | |
local ta = {} | |
local count = 0 | |
for _,si in pairs(st) do | |
local a = si[1]*100+si[2]*10+si[3] | |
local b = si[4]*100+si[5]*10+si[6] | |
local c = si[7]*100+si[8]*10+si[9] | |
--print(a,b,c) | |
if a+b==c then | |
if not ta[b] then | |
ta[a] = true | |
print(sformat("%d + %d = %d",a,b,c)) | |
count = count + 1 | |
end | |
end | |
end | |
print("count:",count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
全排列优化