三.第3部分
61.读入学生成绩,获取最高分best,然后根据下面的规则赋等级值
def quickSort(arr, left=None, right=None):
left = 0 if not isinstance(left,(int, float)) else left
right = len(arr)-1 if not isinstance(right,(int, float)) else right
if left < right:
partitionIndex = partition(arr, left, right)
quickSort(arr, left, partitionIndex-1)
quickSort(arr, partitionIndex+1, right)
return arr
def partition(arr, left, right):
pivot = left
index = pivot+1
i = index
while i <= right:
if arr[i] < arr[pivot]:
swap(arr, i, index)
index+=1
i+=1
swap(arr,pivot,index-1)
return index-1
def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def Top_score(num,array):
arrays = quickSort(array)
for i in range(0,len(array)):
if arrays[i] >= (arrays[len[array] - 1] - 40):
if arrays[i] >= (arrays[len[array] - 1] - 30):
if arrays[i] >= (arrays[len[array] - 1] - 20):
if arrays[i] >= (arrays[len[array] - 1] - 10):
print(f"(第{i + 1}个学生等级为a,成绩为{array[i]})")
else:
print(f"(第{i + 1}个学生等级为b,成绩为{array[i]})")
else:
print(f"(第{i + 1}个学生等级为c,成绩为{array[i]})")
else:
print(f"(第{i + 1}个学生等级为d,成绩为{array[i]})")
else:
print(f"(第{i + 1}个学生等级为f,成绩为{array[i]})")
num = eval(input("请输入学生人数"))
array = list(str(input("请输入以空格分开的学生成绩")).split)
Top_score(num,array)
62.读取1到100之间的整数,然后计算每个数出现的次数
def quickSort(arr, left=None, right=None):
left = 0 if not isinstance(left,(int, float)) else left
right = len(arr)-1 if not isinstance(right,(int, float)) else right
if left < right:
partitionIndex = partition(arr, left, right)
quickSort(arr, left, partitionIndex-1)
quickSort(arr, partitionIndex+1, right)
return arr
def partition(arr, left, right):
pivot = left
index = pivot+1
i = index
while i <= right:
if arr[i] < arr[pivot]:
swap(arr, i, index)
index+=1
i+=1
swap(arr,pivot,index-1)
return index-1
def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def general(num ,array):
arrayss , xsl = [] , 0
arrays = quickSort(array)
for i in range(0,len(array)):
for j in range(0,len(array)):
if array[i] == array[j]:
xsl += 1
print(f"(这个数是{array[i]} ,出现了{xsl}次)")
xsl = 0
print(f"(从小到大的顺序为{arrays})")
num = eval(input("请输入整数的个数"))
array = list(str(input("请输入以空格分开的n个整数")).split)
Top_score(num,array)
63.读入n个数字,并显示互不相同的数(即一个数出现多次,但仅显示一次),数组包含的都是不同的数
def general(num,array)->list:
arrays = list(set(array))
return arrays
num = eval(input("请输入数字的个数"))
array = list(str(input("请输入以空格分开的数字")).split)
arrays = general(num,array)
print("输入的列表",array)
print("去重后的列表",arrays)
64.输入n个数字,求该n个数字的最大公约数
def greatest_common_divisor(num1,array):
for i in range(0,len(array)):
while array[i + 1]!=0:
array[i],array[i + 1]=array[i + 1],array[i]%array[i + 1]
array[i + 1] = array[i]
return array[len(array) - 2]
num = eval(input("请输入数字的个数"))
array = list(str(input("请输入以空格分开的数字")).split)
xsl = greatest_common_divisor(num,array)
print("这一组数的最大公约数为{xsl}")
65.对给定的数组进行随机打乱,并输出打乱后的结果
import random
def shuffle_array(arr):
# 使用random.shuffle函数对数组进行随机打乱
random.shuffle(arr)
return arr
array = list(str(input("请输入以空格分隔的数组元素").split))
arrays = shuffle_array(array)
print("乱序后的数组为",arrays)
66.对给定的数组进行判断,判断其数组元素是否非单调递减
def orderly(num ,array):
a = 0
for i in range(0,num):
for j in range(0,num):
if array[i]<=array[j] and i == num-1:
print("该列表是以非单调递减的形式排列的")
a = True
if a != True:
print("该列表不是以非单调递减的形式排列的")
def general(yp):
for i in range(0,yp):
xsl = eval(input("请输入测试组号"))
array = list(str(input("请输入以空格为分割的该组数据")))
orderly(xsl ,array)
tw = eval(input("请输入测试数据组数"))
general(tw)
67.输入两个英文单词,判断其是否为相似词,所谓相似词是指两个单词包含相同的字母
def Similar_words(numss,numss):
arrays , arrayss = [] , []
arrays = list(str(nums))
arrayss = list(str(numss))
if len(arrays) > len(arrayss):
for i in range(0,len(arrayss)):
for j in range(0,len(arrays)):
if arrayss[i]!=arrays[j]:
return "NO"
elif i == len(arrayss):
return "Yes"
xsl = eval(input("请输入第一个单词"))
yp = eval(input("请输入第二个单词"))
tw = Similar_words(xsl,yp)
print(tw)
68.豆机器
def simulate_bean_machine(ball_count, slot_count):
import random
slots = [0] * (slot_count + 1) # 初始化每个槽子中的小球数量为0
for _ in range(ball_count):
position = slot_count // 2 # 设置小球初始位置在中间槽子
while True:
direction = random.choice([-1, 1]) # 随机选择左右方向
position += direction
if position < 0 or position > slot_count: # 如果小球到达边界,则停止运动
break
slots[position] += 1 # 在最终位置增加小球数量
print("小球路径模拟:")
for i in range(slot_count + 1):
print("-" * i + "O" + "-" * (slot_count - i)) # 打印每个小球的路径模拟
print("\n每个槽子中的小球数量:")
for i in range(slot_count + 1):
print(f"槽子{i}: {slots[i]}") # 打印每个槽子中小球的个数
# 提示用户输入小球的个数和机器的槽数
ball_count = int(input("请输入小球的个数:"))
slot_count = int(input("请输入机器的槽数:"))
simulate_bean_machine(ball_count, slot_count)
69.更衣室问题
import math
def find_open_lockers(n):
open_lockers = []
for i in range(1, int(math.sqrt(n)) + 1):
open_lockers.append(i * i)
return open_lockers
open_lockers = find_open_lockers(100)
print("最后打开的更衣室编号:", open_lockers)
70.合并两个有序数组
def orderly(arrays ,arrayss)->list:
array = arrays.extend(arrayss)
arrayx = array.sort
return arrayx
arrays = list(str(input("请输入以空格分隔的第一个数组元素")))
arrayss = list(str(input("请输入以空格分隔的第二个数组元素")))
arrayx = orderly(arrays , arrayss)
print(arrays,arrayss)
print(f("合并排序后的列表{arrayx}"))
71.数组划分
def divide(array : list)->list:
arrays , arrayss , arrayx = [] , [] , []
for i in range(0,len(array)):
if i > array[0]:
arrays.append(i)
array.remove(i)
elif i < array[0]:
arrayss.append(i)
array.remove(i)
arrayx(arrays,arrayss,array)
return arrayx
array = list(str(input("请输入以空格分隔的列表元素")).split)
arrayx = divide(array)
print(arrayx[0],arrayx[1],arrayx[0])