第1关:三位水仙花数

1
2
3
4
5
6
7
8
9
10
n = input()

num = 0
for i in n:
num += int(i) ** len(n)

if int(n) == num:
print("True")
else:
print("False")

第2关:判断闰年

1
2
3
4
5
6
n = int(input())

if n % 4 == 0 and n % 100!= 0 or n % 400 == 0:
print("True")
else:
print("False")

第3关:考试成绩统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
result = {"优秀": 0, "良": 0, "中": 0, "及格": 0, "不及格": 0}

for it in input().split(","):
if int(it) >= 90:
result["优秀"] += 1
elif int(it) >= 80:
result["良"] += 1
elif int(it) >= 70:
result["中"] += 1
elif int(it) >= 60:
result["及格"] += 1
else:
result["不及格"] += 1

print(result)

第4关:简单的数字排序

1
2
3
4
5
6
7
8
9
10
a, b, c = input().split()

if a > b:
a, b = b, a
if a > c:
a, c = c, a
if b > c:
b, c = c, b

print(a, b, c)

第5关:求一元二次方程的解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
a, b, c = map(int, input().split())

discriminant = b**2 - 4 * a * c

if a == 0:
print("不是二次方程")
elif discriminant > 0:
root1 = (-b + discriminant**0.5) / (2 * a)
root2 = (-b - discriminant**0.5) / (2 * a)
print(f"实根1:{root1:.2f}")
print(f"实根2:{root2:.2f}")
elif discriminant == 0:
root = -b / (2 * a)
print(f"有两个相等的实根:{root:.2f}")
else:
real_part = -b / (2 * a)
imaginary_part = (-discriminant) ** 0.5 / (2 * a)
root1 = f"{real_part:.2f}+{imaginary_part:.2f}j"
root2 = f"{real_part:.2f}-{imaginary_part:.2f}j"
print(f"虚根1:{root1}")
print(f"虚根2:{root2}")

第6关:输入一个年份和月份,打印出该月份有多少天。

1
2
3
4
5
6
7
8
9
10
11
year, mouth = map(int, input().split())

if mouth == 2:
if year % 4 == 0 and year % 100!= 0 or year % 400 == 0:
print(29)
else:
print(28)
elif mouth in [4, 6, 9, 11]:
print(30)
else:
print(31)