相求n范围内有多少对fancy pairs(比如123和31就是fancy pair,因为123中最大的数和31中最大的数一样是3,最小的数和31中最小的数一样是1)。不知道为什么运行不对
def num_fancy_pairs(n):
"""
A function that returns the number of fancy pairs described in
the write-up.
---
Parameters:
n: the upper bound
---
Return an integer indicating the number of valid pairs
>>> num_fancy_pairs(1)
1
>>> num_fancy_pairs(20)
12
>>> num_fancy_pairs(50)
33
"""
# YOUR CODE GOES HERE #
n=0
for x,y in range(n):
if max(list(str(x)))=max(list(str(y))) and min(list(str(x)))=min(list(str(y))):
n+=1
return n
你传的参数是n,然后让n=0相当于不循环了
def num_fancy_pairs(n):
t=0
for x in range(1,n+1):
for y in range(1,n+1):
if x<11 and y<11:
if max(list(str(x)))==max(list(str(y))) and min(list(str(x)))==min(list(str(y))) :
t+=1
else:
if max(list(str(x)))==max(list(str(y))) and min(list(str(x)))==min(list(str(y))) and x!=y:
t+=1
return t
n=int(input())
print(num_fancy_pairs(n))
用==吧,=是赋值
range只是返回一个可遍历序列,只能同时赋值一个,所以
for x,y in range(n):
是错的
另外
A function that returns the number of fancy pairs described in
the write-up.
这个描述的功能是怎么计算的呢,你描述一下,可能我就帮你弄出来了
用==吧,=是赋值
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!