标签:输出 算数 位置 选择 target NPU python 出现 视频
1. 统计数字:计算数字k在0到n中出现的次数,k可能是0-9的一个值
n=12,k=1,在[0,1,2,3,4,56,7,8,9,10,11,12],发现1出现了5次(1,10,11,12)
def countK(k,num):
count = 0
listK = list(range(num+1))
for item in listK:
# print("item = ",item)
for i in str(item):
# print("i = ",i)
if i == str(k):
count += 1
# print("count=",count)
return count
if __name__ == "__main__":
k,num = map(int,input().split(" "))
print(countK(k,num))
2.leetcode 754到达终点的数字
在一个无限长的数轴上,你站在0的位置,终点在target的位置,每次你可以选择向左或者向右移动。第n次移动(从1开始),可以走n步
返回到达终点需要的最小移动次数
target = 3,
输出:2
解释:第一次移动,从0-1
第二次移动,从1-3
def move(target):
s = 0
i = 1
while True:
s += i
if s>=target and (s-target)%2 == 0:
return i
i += 1
if __name__ == "__main__":
target = int(input())
print(move(target))
标签:输出 算数 位置 选择 target NPU python 出现 视频
原文地址:https://www.cnblogs.com/ivyharding/p/11432328.html