local orig = io.read("*n") local t = {} while(0 < orig) do local a = orig % 10 table.insert(t, a) orig = math.floor(orig / 10) end local maxdig = 1 for i = 2, #t do if(t[maxdig] < t[i]) then maxdig = i end end local reppos = maxdig for i = #t, maxdig, -1 do if(t[i] < t[maxdig]) then reppos = i break end end t[reppos], t[maxdig] = t[maxdig], t[reppos] for i = #t, 1, -1 do io.write(t[i]) end io.write("\n")