API练习题的链接如下 :
https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7
拖了好久,在更新几道,正在自己看网站学习python中,所以,目前打的代码都是根据自己学的内容写的,可能不是最优代码。欢迎在评论区交流。
目录
L1-040 最佳情侣身高差
import math
# 定义函数
def is_shen_gao(sex,h):
if sex == 'F':
perfect = h * 1.09
print("%.2f"%(perfect))
elif sex == 'M':
perfect = h/1.09
print("%.2f"%(perfect))
# 调用函数
N = int(input())
if N <= 10:
numbers = []
for i in range(N):
sex,h = input().split()
h = float(h)
numbers.append((sex,h))
if 1.0 < h <= 3.0:
for y in numbers:
is_shen_gao(y[0], y[1])
else:
print("身高不合理")
else:
print("N大于10")
L1-041 寻找250
N = input()
numbers = list(map(int, N.split()))
position = []
for p,i in enumerate(numbers):
if i == 250:
position = p+1
break
print(f"{position}")
L1-007 念数字
N = input().split()
pin_yin = {
'0': 'ling',
'1': 'yi',
'2': 'er',
'3': 'san',
'4': 'si',
'5': 'wu',
'6': 'liu',
'7': 'qi',
'8': 'ba',
'9': 'jiu',
}
result = []
B = True
for num in N:
if num.startswith('-'):
result.append('fu')
num = num[1:]
for d in num:
result.append(pin_yin[d])
print(" ".join(result))
L1-068 调和平均
# 定义函数,求调和平均
def tiao_he(numbers):
sum = 0
for x in numbers:
for y in x:
sum += (1/y)/N
result = float(1/sum)
return result
N = int(input())
if N <=1000:
numbers = []
num = list(map(float,input().split()))
numbers.append(num)
# 调用函数
r = tiao_he(numbers)
print("%.2f"%(r))
else:
print("N大于1000")
L1-055 谁是赢家
# 定义函数,判断赢家
def prime_winner(Pa,Pb,a,b):
if Pa > Pb and a > 0:
P1 = Pa
if a > b:
P2 = a
print(f"The winner is a: {P1} + {P2}")
else:
P2 = a
print(f"The winner is a: {P1} + {P2}")
else:
P1 = Pb
if a > b:
P2 = b
print(f"The winner is b: {P1} + {P2}")
else:
P2 = b
print(f"The winner is b: {P1} + {P2}")
Pa,Pb = map(int,input().split())
if Pa <= 1000 and Pb <= 1000:
N = input().split()
numbers = []
for i in (N):
numbers.append((i))
count1 = 0 # 记录给b选手投票的评委票数
count2 = 0 # 记录给a选手投票的评委票数
for x in numbers:# 通过循环去进行票数计算
if x == '1':
count1 += 1
else:
count2 += 1
a = count2
b = count1
# 调用函数
prime_winner(Pa,Pb,a,b)
L1-084 拯救外星人
A,B = map(int,input().split())
N = A + B
s=1
for i in range(1,N):
i = i+1
s = i * s
print(s)
L1-099 帮助色盲
# 定义函数,判断执行动作
def prime_zhixing(A,B):
if A == '0'and B == '0':
print("biii\nstop")
elif A == '0'and B == '1':
print("-\nstop")
elif A == '1'and B == '0':
print("dudu\nmove")
elif A == '1'and B == '1':
print("-\nmove")
elif A == '2'and B == '0':
print("-\nstop")
elif A == '2'and B == '1':
print("-\nstop")
A, B = map(str,input().split())
prime_zhixing(A,B)
在这里,需要注意的就是灯是黄灯的时候,不管有没有同向的人,都需要停下来。