1判断三角形
a = int(input("请输入第一条边的长度:"))
b = int(input("请输入第二条边的长度:"))
c = int(input("请输入第三条边的长度:"))
def sanjiaoxing(a, b, c):
if b + c <= a and a + b <= c and a + c <= b:
print("0")
else:
if a == b == c:
print("1")
elif c * c + b * b == a * a or a * a + b * b == c * c or c * c + a * a == b * b:
print("3")
elif a == b or b == c:
print("2")
else:
print("4")
sanjiaoxing(a, b, c)
2.角谷定理
# 输入一个自然数,若为偶数,则把它除以2,
# 若为奇数,则把它乘以3加1。
# 经过如此有限次运算后,总可以得到自然数值1
# 。求经过多少次可得到自然数1。
def jiaogu(n):
a = []
count = 0
while n != 1:
if n % 2 == 0:
n = n / 2
a.append(n)
count += 1
else:
n = n * 3 + 1
a.append(n)
count += 1
print(count)
print(a)
n = int(input('请输入一个正整数:'))
if n <= 0:
print('输入错误')
jiaogu(n)
.3.随机生成成绩-判断等级并求最高分最低分
# 开发者:林*
# 开发时间:2022/10/14 8:14
import random
def get_level(score):
if 90 < score <= 100:
return '优秀'
elif 80 < score <= 90:
return '良好'
elif 70 < score <= 80:
return '中等'
elif 60 < score <= 70:
return '及格'
else:
return '不及格'
MInScore = 101
MaxScore = 0
for i in range(20):
score = random.randint(0, 100)
level = get_level(score)
MaxScore = max(MaxScore,score)
MInScore = min(MInScore,score)
print("成绩: %s, 等级: %s" % (score, level))
print("最高分%d" % MaxScore)
print("最低分%d" % MInScore)
4.奇数在前偶数在后
# 开发者:林*
# 开发时间:2022/10/14 9:09
def reorder_odd_event(arry):
if len(arry) < 2:
return arry
begin, end = 0, len(arry) - 1
while begin < end:
if arry[begin] % 2 == 1:
begin += 1
if arry[end] % 2 == 0:
end -= 1
if arry[begin] % 2 == 0 and arry[end] % 2 == 1:
arry[begin], arry[end] = arry[end], arry[begin]
begin += 1
end -= 1
return arry