题目描述:给定一个数字,按照原有数字中的各个位上面的数字进行组合,求出比原有数字大的数中最小的数。例如:1234 -> 1243 ,1243 ->1324 ,1324 -> 1342。
解题思路:
1.先将原有数字中的个,十,百,千…位上面的数字取出来,并进行全排列。
2.将全排列得到的数字再进行可以得到由原来数字组合组成的所有的数字。
3.将第二部得到的数字排序,找出题目要求的数。
代码:
def full_arrage(ls, res_ls, index):
if index>=len(ls):
res_ls.append(int(''.join(ls)))
return
for i in range(index, len(ls)):
cur_e1 = ls[index]
cur_e2 = ls[i]
ls[i],ls[index] = cur_e1, cur_e2
full_arrage(ls, res_ls, index+1)
ls[index] = cur_e1
ls[i] = cur_e2
def find_target(ls, input):
for i, e in enumerate(ls):
if e==input:
return ls[i+1]
input=3451
ls = list(str(input))
res_ls =[]
full_arrage(ls, res_ls, 0)
res = sorted(res_ls)
print(len(res), res)
target = find_target(res, input)
print(target)