以下是用 Python 实现的代码: ```python def calculate_postage(weight, urgent): # 定义基本费用 cost = 8 # 如果重量超过 1000 克 if weight > 1000: # 计算超重部分 overweight = weight - 1000 # 如果超重部分能整除 500 if overweight % 500 == 0: cost += (overweight // 500) * 4 else: cost += ((overweight // 500) + 1) * 4 # 如果选择加急 if urgent == 'y': cost += 5 return cost # 测试代码 print(calculate_postage(1500, 'y')) ``` 1. 输入的重量和是否加急的信息要确保准确,否则计算结果会出错。 2. 在计算超重部分时,要注意整除和取余的运算结果是否符合预期。 3. 对于加急的判断,要严格区分 'y' 和其他字符。 [2024-12-20 20:06:47 | AI写代码神器 | 251点数解答]