:1公斤内12元,超过的部分每0.5公斤4元,不足0.5公斤的按0.5公斤算,如果选择加急多收10元。输入邮件质量,输入个字符 (y表示加急,n表示不加急) 计算邮费
时间: 2023-05-16 22:06:10 浏览: 264
以下是计算邮费的 Python 代码:
```
weight = float(input("请输入邮件质量(单位:千克):"))
urgent = input("是否加急?(y/n):")
if weight <= 1:
fee = 12
else:
fee = 12 + (weight - 1) // 0.5 * 4
if (weight - 1) % 0.5 != 0:
fee += 4
if urgent == 'y':
fee += 10
print("邮费为:", fee, "元")
```
请注意,这是一个 Python 代码示例,如果您使用的是其他编程语言,需要根据语言特性进行相应修改。
相关问题
用C++语言编写:根据邮件的重量和是否选择加急计算邮费。 计算规则:重量在 1000 克以内(包括 1000 克),基本费 8 元。 超过 1000 克的部分,每 500克加收超重费 4 元,不足 500克部分按 500克计算; 如果选择加急,多收5 元
以下是基于题目要求的 C++ 代码实现:
```cpp
#include <iostream>
using namespace std;
int main() {
int weight;
bool isUrgent;
double price = 8.0;
cout << "Please enter the weight of the mail in grams: ";
cin >> weight;
if (weight <= 1000) {
// 重量在 1000 克以内(包括 1000 克),基本费 8 元
cout << "The basic fee for this mail is 8 yuan." << endl;
} else {
// 超过 1000 克的部分,每 500 克加收超重费 4 元,不足 500 克部分按 500 克计算
int extraWeight = weight - 1000;
int extraFee = (extraWeight / 500) * 4;
if (extraWeight % 500 != 0) {
extraFee += 4;
}
price += extraFee;
cout << "The basic fee for this mail is 8 yuan. "
<< "The extra weight fee is " << extraFee << " yuan." << endl;
}
cout << "Would you like to choose urgent service? (0 for no, 1 for yes) ";
cin >> isUrgent;
if (isUrgent) {
// 如果选择加急,多收5元
price += 5.0;
cout << "The urgent service fee is 5 yuan." << endl;
}
cout << "The total price for this mail is " << price << " yuan." << endl;
return 0;
}
```
你可以根据自己的需要进行修改和调整。
计算邮费。计算规则:重量在1000克以内(包括1000克), 基本费8元。超过1000克的部分,每500克加收超重费4元,不足500克部分按500克计算;如果用户选择加急,多收5元。帮我写代码
好的,我可以帮助你写代码。以下是 Python 代码:
```
def calculate_postage(weight, is_urgent):
base_fee = 8
if weight > 1000:
overweight_fee = (weight - 1000) // 500 * 4
if (weight - 1000) % 500 != 0:
overweight_fee += 4
else:
overweight_fee = 0
if is_urgent:
return base_fee + overweight_fee + 5
else:
return base_fee + overweight_fee
weight = int(input("请输入物品重量(克): "))
is_urgent = input("是否选择加急(yes/no): ") == "yes"
postage = calculate_postage(weight, is_urgent)
print(f"邮费: {postage} 元")
```
这段代码实现了根据物品重量和是否加急计算邮费的功能。希望对您有帮助!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)