Skip to content

Commit 1d87576

Browse files
committed
feat(mooc): week4
1 parent 4435c8d commit 1d87576

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
四位玫瑰数
3+
描述‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
4+
5+
四位玫瑰数是4位数的自幂数。自幂数是指一个 n 位数,它的每个位上的数字的 n 次幂之和等于它本身。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
6+
7+
例如:当n为3时,有1^3 + 5^3 + 3^3 = 153,153即是n为3时的一个自幂数,3位数的自幂数被称为水仙花数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
8+
9+
请输出所有4位数的四位玫瑰数,按照从小到大顺序,每个数字一行。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
10+
11+
输入输出示例
12+
输出仅表示格式,不表示对错。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
13+
14+
'''
15+
16+
17+
for i in range(1000, 10000):
18+
result = 0
19+
s = str(i)
20+
for c in s:
21+
result += int(c)**4
22+
if i == result:
23+
print(result)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
300371587123826141
3+
100以内素数之和
4+
描述
5+
求100以内所有素数之和并输出。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
6+
素数指从大于1,且仅能被1和自己整除的整数。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
7+
8+
提示:可以逐一判断100以内每个数是否为素数,然后求和。
9+
'''
10+
11+
12+
def isprime(num):
13+
for i in range(2, int(pow(num, 0.5))+1):
14+
if num % i == 0:
15+
return False
16+
return True
17+
18+
19+
result = 0
20+
for i in range(2, 101):
21+
if isprime(i):
22+
result += i
23+
print(result)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
0030003300371587018045538
3+
实例5:身体质量指数BMI
4+
5+
描述
6+
这是"实例"题,与课上讲解实例相同,请作答检验学习效果。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
7+
BMI :Body Mass Index 国际上常用的衡量人体肥胖和健康程度重要标准,主要用于统计分析‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
8+
定义:BMI = 体重 (kg) /身高2(m2)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
9+
获取用户输入的体重和身高值,计算并给出国际和国内的 BMI 分类‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
10+
‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
11+
(1) 混合计算并给出国际和国内的 BMI 分类;‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
12+
(2) 使用input()获得测试用例输入时,不要增加提示字符串。
13+
'''
14+
15+
height, weight = eval(input())
16+
BMI = weight / pow(height, 2)
17+
print("BMI数值为:{:.2f}".format(BMI))
18+
19+
BMI_i = "" # BMI国际
20+
BMI_c = "" # BMI中国
21+
22+
if BMI < 18.5:
23+
BMI_i = "偏瘦"
24+
BMI_c = "偏瘦"
25+
elif BMI < 24:
26+
BMI_i = "正常"
27+
BMI_c = "正常"
28+
elif BMI < 25:
29+
BMI_i = "正常"
30+
BMI_c = "偏胖"
31+
elif BMI < 28:
32+
BMI_i = "偏胖"
33+
BMI_c = "偏胖"
34+
elif BMI < 30:
35+
BMI_i = "偏胖"
36+
BMI_c = "肥胖"
37+
else:
38+
BMI_i = "肥胖"
39+
BMI_c = "肥胖"
40+
41+
print("BMI指标为:国际'{}',国内'{}'".format(BMI_i, BMI_c))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
30003300371587019990474
3+
实例6:圆周率的计算‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮
4+
描述
5+
这是"实例"题,与课上讲解实例相同,请作答检验学习效果。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
6+
7+
求解圆周率可以采用蒙特卡罗方法,在一个正方形中撒点,根据在1/4圆内点的数量占总撒点数的比例计算圆周率值。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
8+
请以123作为随机数种子,获得用户输入的撒点数量,编写程序输出圆周率的值,保留小数点后6位。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
9+
10+
输入输出示例
11+
输入 输出
12+
示例 1 1024 3.218750
13+
'''
14+
15+
from random import random, seed
16+
17+
count = int(input())
18+
seed(123)
19+
20+
hit = 0
21+
for i in range(0, count):
22+
x, y = random(), random()
23+
dist = pow(x**2 + y**2, 0.5)
24+
if dist <= 1:
25+
hit += 1
26+
pi = (hit / count)*4
27+
print("{:.6f}".format(pi))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
整数的加减和
3+
描述
4+
编写程序计算如下数列的值:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
5+
1-2+3-4...966‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
6+
其中,所有数字为整数,从1开始递增,奇数为正,偶数为负
7+
'''
8+
9+
result = 0
10+
for i in range(1, 967):
11+
if i % 2 == 0:
12+
result -= i
13+
else:
14+
result += i
15+
print(result)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
00371587024715377
3+
三位水仙花数
4+
描述
5+
"水仙花数"是指一个三位整数,其各位数字的3次方和等于该数本身。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬
6+
7+
例如:ABC是一个"3位水仙花数",则:A的3次方+B的3次方+C的3次方 = ABC。
8+
9+
请按照从小到大的顺序输出所有的3位水仙花数,请用"逗号"分隔输出结果。
10+
11+
'''
12+
13+
result = []
14+
for i in range(100, 1000):
15+
a = int(str(i)[0])
16+
b = int(str(i)[1])
17+
c = int(str(i)[2])
18+
if a**3 + b**3 + c**3 == i:
19+
result.append(str(i))
20+
print(",".join(result))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
用户登录的三次机会
3+
描述
4+
给用户三次输入用户名和密码的机会,要求如下:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
5+
1)如输入第一行输入用户名为‘Kate’,第二行输入密码为‘666666’,输出‘登录成功!’,退出程序;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
6+
2)当一共有3次输入用户名或密码不正确输出“3次用户名或者密码均有误!退出程序。”。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬
7+
8+
输入输出示例
9+
输入 输出
10+
示例 1 Kate
11+
666666 登录成功!
12+
13+
kate
14+
123
15+
alice
16+
456
17+
john
18+
111111 3次用户名或者密码均有误!退出程序。
19+
'''
20+
21+
name, password, i, flag = 'Kate', '666666', 3, 0
22+
while i > 0:
23+
i -= 1
24+
n = input()
25+
p = input()
26+
if n == name and p == password:
27+
flag = 1
28+
print("登录成功!")
29+
break
30+
if flag == 0:
31+
print("3次用户名或者密码均有误!退出程序。")

0 commit comments

Comments
 (0)