2025专科生必备!8个降AI率工具测评榜单
2025/12/24 18:13:02
from sys import stdout # 导入sys模块中的stdout对象(标准输出流) n = int(input("input number:\n")) # 提示用户输入一个数字,并转换为整数类型 print("n=%d"%n) # 打印输入的数字n的值 # 外层循环:从2开始到n(包含n)的所有整数 for i in range(2, n+1): # 内层循环:当n不等于1时持续执行 while n != 1: # 检查n是否能被i整除 if n % i == 0: stdout.write(str(i)) # 将当前因数i写入标准输出(不换行) stdout.write("*") # 写入乘号分隔符(不换行) n = n / i # 更新n的值为n除以i的结果(浮点数) else: break # 如果不能整除,跳出内层while循环 print("%d"%n) # 打印最后剩余的n值(此时应为1)结果:
input number:
7868
n=7868
2*2*7*281*1
def prime_factors_1(n): factors = [] # 存储质因数的列表 d = 2 # 从最小质数2开始 while n > 1: if n % d == 0: # 若能被整除 factors.append(d) # 将除数加入质因数列表 n //= d # 更新n为商 else: d += 1 # 除数加1继续尝试 return factors num = int(input("请输入数字:")) print(f"{num} = {' × '.join(map(str, prime_factors_1(num)))}")结果:
请输入数字:90
90 = 2 × 3 × 3 × 5
def prime_factors_3(n, factor=2, result=None): if result is None: # 初始化结果列表 result = [] if n == 1: # 递归终止条件 return result if n % factor == 0: result.append(factor) return prime_factors_3(n // factor, factor, result) else: return prime_factors_3(n, factor + 1, result) num = int(input("请输入数字:")) print(f"{num} = {' × '.join(map(str, prime_factors_3(num)))}")结果:
请输入数字:90
90 = 2 × 3 × 3 × 5
利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分一下的用C表示。
score = int(input('input your score: ')) if score >= 90: grade = 'A' elif score >= 60: grade = 'B' else: grade = 'C' print('%d belongs to %s' % (score, grade)) # %d:整数 # %s:字符串 # %f:浮点数(如 %.2f 保留两位小数) # 替代:print(f'{score} belongs to {grade}')结果:
input your score: 90
90 belongs to A
其他方法:
def get_grade(score): thresholds = [90, 60] grades = ['A', 'B', 'C'] for i, threshold in enumerate(thresholds): if score >= threshold: return grades[i] return grades[-1] score = int(input('input your score: ')) print(f'{score} belongs to {get_grade(score)}')结果:
input your score: 78
78 belongs to B
score = int(input('input your score: ')) # 计算分数段索引:90+→0, 60-89→1, <60→2 index = min(2, max(0, (90 - score) // 30 + 1)) grade = ['A', 'B', 'C'][index] print(f'{score} belongs to {grade}')结果:
input your score: 67
67 belongs to B