鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: zltzlt

[已解决]Python:每日一题 311

[复制链接]
发表于 2020-1-23 10:12:04 | 显示全部楼层
  1. def func311(n):
  2.     list1 = list(str(n))
  3.     list2 = sorted(list(str(n)), reverse= True)
  4.     i = 0
  5.     while i < len(list1):
  6.         if list1[i] >= list2[i]:
  7.             i += 1
  8.             continue
  9.         else:
  10.             j = i+1
  11.             while j < len(list1):
  12.                 if list1[j] < list2[j]:
  13.                     j += 1
  14.                     continue
  15.                 else:
  16.                     temp = list1[i]
  17.                     list1[i] = list1[j]
  18.                     list1[j] = temp
  19.                     return int(''.join(list1))
  20.     if i == len(list1) or j == len(list1):
  21.         return n
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-22 11:42:08 | 显示全部楼层
  1. from itertools import permutations

  2. def fun311(x):
  3.     x = str(x)
  4.     combin_list = []
  5.     for i in permutations(x):
  6.         count = 0
  7.         for j in range(len(i)):
  8.             if x[j] != i[j]:
  9.                 count += 1
  10.             if count > 2:
  11.                 break
  12.         if count <= 2:
  13.             combin_list.append(int(''.join([n for n in i])))
  14.     return max(combin_list)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 07:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表