题目
编写一个函数 max_val,接收两个整数作为参数,返回二者中的较大值。
输入格式:一行输入两个整数 a 和 b,以空格分隔。
输出格式:输出一个整数,表示两数中的较大者,末尾换行。
数据范围与约定:-109 ≤ a, b ≤ 109。
思路与算法
1. 条件表达式
函数内使用单行三元表达式 a if a > b else b 返回结果。
2. 逻辑抽象
封装基础比较逻辑,增强业务代码可读性。
完整程序
s = input()
# 统计字母、数字、空格及其它字符出现次数
letters = digits = spaces = others = 0
for ch in s:
if ch.isalpha():
letters += 1
elif ch.isdigit():
digits += 1
elif ch == ' ':
spaces += 1
else:
others += 1
print(f'{letters} {digits} {spaces} {others}')运行示例
输入:
8 5输出:
8