Python:每日一题 361
今天的题目:给定一个非负整数 n,找出小于或等于 n 的单调递增整数中最大的整数。
说明:当且仅当每个相邻位数上的数字 x 和 y 满足 x <= y 时,称这个整数是单调递增的。
示例 1:
输入:n = 10
输出:9
示例 2:
输入:n = 1234
输出:1234
示例 3:
输入:n = 332
输出:299
{:10_298:}欢迎大家一起答题!{:10_298:} 没看懂@zltzlt _2_ 发表于 2020-3-28 18:44
没看懂@zltzlt
我估计你不明白单调递增的整数的概念
比如,289 是单调递增整数,因为 2 <= 8 <= 9
456 也是
但是 1231 不是 zltzlt 发表于 2020-3-28 18:46
我估计你不明白单调递增的整数的概念
比如,289 是单调递增整数,因为 2
那一位数字呢? 只有一个数的话是默认递增还是没有 本帖最后由 BngThea 于 2020-3-29 17:27 编辑
def func(num):
bits,tmp = 0,num
numbers = []
while tmp:
numbers.append(tmp%10)
tmp //= 10
bits +=1
if bits == 1:
return num
else:
numbers = list(reversed(numbers))
for i in range(bits-1):
if numbers > numbers:
numbers -= 1
k = i
while k > 0:
if numbers > numbers:
numbers = 9
numbers -= 1
k = k-1
else:
break
for j in range(i+1,bits):
numbers = 9
return int(''.join(map(str,numbers)))
else:
return num {:10_257:}{:10_257:}{:10_257:}吃瓜群众路过,看过了,在大神的提示下,还是找不到规律。 本帖最后由 塔利班 于 2020-3-29 13:32 编辑
def f361(x):
t,b,mi=0,str(x),0
for i in range(len(b)-1):
if b<b:
t+=1
elif b>b:
if t:
mi=i
return int(str(int(b[:mi+1])-1)+'9'*(len(b)-1-mi))
else:
if t:
t=0
mi=i
return x 本帖最后由 TJBEST 于 2020-3-28 22:09 编辑
写个效率低的
def fun361(num):
def split(num):
result = []
while num:
result.append(num%10)
num = num // 10
result.reverse()
return result
if num < 10:
return num
bits = split(num)
M = len(bits)
E_position = M
G_position = M
state = False
for i in range(0,M-1):
if bits == bits:
if state == False:
E_position = i
state = True
else:
pass
elif bits < bits:
if state == False:
pass
else:
state = False
E_position = M
elif bits > bits:
break
for i in range(0,M-1):
if bits > bits:
G_position = i
break
if G_position > M - 1:
return num
else:
temp = 0
position = min()
for i in range(0,position+1):
temp += bits*(10**(M-1-i))
return temp - 1 fan1993423 发表于 2020-3-28 19:21
只有一个数的话是默认递增还是没有
一位数是 def func(n):
while n:
list1 = list(str(n))
if list1 == sorted(list1):
return n
n -= 1 本帖最后由 flamezyy 于 2020-3-28 20:18 编辑
def f361(n):
if n < 10:
return n
str_n = str(n)
result = ''
for i in range((length := len(str_n))-1):
if str_n <= str_n:
continue
elif i == 0:
return int(''.join() - 1),'9'*(length-1)]))
elif (o := str(int(str_n)-1)) >= str_n:
for each in str_n[:i]:
result += each
result += o + '9' * (length-1-i)
else:
if i == 1:
if str_n == str_n:
result += str(int(str_n)-1)+ '99'
else:
result += str_n + '99'
else:
for t in list(range(i)):
if str_n > str_n:
for each in str_n[:t+1]:
result += each
result += str(int(str_n)-1)+ '9' * (length-2-t)
return int(result)
return int(str(int(str_n)-1)+ '9' * (length-1))
return int(result)
else:
return n
有待优化,但脑子已经晕了{:10_291:} def f361(n):
str_n_list=list(str(n))
if str_n_list==sorted(str_n_list):
return n
else:
while str_n_list!=sorted(str_n_list):
str_n_list = list(str(n))
n-=1
return n+1 本帖最后由 fan1993423 于 2020-3-28 20:35 编辑
def fun361(n):
if n in range(10):return n
a,b=divmod(n,10)
flag=True
while a:
if a%10>b:
a-=1
flag,c=False,a
a,b=divmod(a,10)
return n if flag else int(str(c)+'9'*(len(str(n))-len(str(c)))) 本帖最后由 一个账号 于 2020-3-29 11:26 编辑
def func(n):
for i in range(n, 0, -1):
last = 0
for j in str(i):
if last > (last := int(j)): break
else:
return i zltzlt 发表于 2020-3-28 20:08
一位数是
0 算不算? 本帖最后由 kinkon 于 2020-3-28 23:37 编辑
def f361(n):
while list(str(n)) != sorted(str(n)):
n -= 1
return n
上面的可能超时了,试试下面的
def f361(n):
num = list(str(n))
tmp = []
for i in range(len(num)-2,-1,-1):
if num > num:
num = str(int(num)-1)
a = len(num) - 1
tmp = []
while a > i:
num = '9'
tmp.append(num)
a -= 1
else:
tmp.append(num)
tmp.append(num)
return int(''.join(tmp)[::-1])
再优化一下
def p361(n):
num = list(str(n))
s = ''
for i in range(len(num)-2,-1,-1):
if num > num:
num = str(int(num)-1)
s = ''
s += '9'* len(num)
else:
s += num
s += num
return int(s[::-1]) n = 332
s = str()
def fun361(n):
while True:
s = str(n)
length = len(s)
active = False
if length == 1:
active = True
break
else:
for i in range(length-1):
if int(s) <= int(s):
active = True
else:
active = False
break
if active:
break
else:
n -= 1
return n
print(fun361(n)) 本帖最后由 mdphd 于 2020-3-28 21:39 编辑
def f361(n):
a = str(n)
b = ''
c = 0
for i in range(len(a)-1):
if a <= a:
b += a
else:
b += str(int(a) - 1)
c = 1
break
if c == 1:
b = b.ljust(len(a), '9')
elif c == 0:
b = a
int(b)
return int(b)
请楼主帮忙看看我写的359、360,谢谢!
不知道为什么直到刚刚才刷新出来这三天的每日一题,才做的……{:10_285:} def findmax(num):
i = 1
while num//10**i != 0:
i += 1
ws = i - 1
ws2 = i - 1
a = []
while 1:
j = num//10**ws
a.append(j)
if j == 0:
num = num
else:
num = num - j*10**ws
ws -= 1
if ws == -1:
break
return a
def func(num):
while 1:
list1 = findmax(num)
list2 = list1.copy()
list2.sort()
if list2 == list1:
break
else:
num -= 1
l = len(list1)
sum1 = 0
for i in range(l):
sum1 += list1*10**(l-1)
l -= 1
print(sum1)
func(332)
我的代码好长{:5_100:}