一道作业题
上海市的某快递公司根据投送目的地距离公司的远近,将全国划分成5个区域:快递费按邮件重量计算,由起重费用、续重费用两部分构成:(1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费; (2) 同城起重资费10元,续重3元/公斤; (3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元; (4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。
相关知识(略)
编程要求
编写程序,从键盘输入邮件的目的区域编码和重量,计算并输出运费,计算结果保留2位小数。
提示
续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。
输入
用逗号分隔的两个数字,第一个表示区域、第二个是重量:"%d,%f"
输出
价格的输出格式:"Price: %.2f\n" 区域错误的提示信息:"Error in Area\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
测试说明
样例输入: 4,4.5
样例输出: Price: 55.00
程序运行结果示例1:
输入:4,4.5 输出:Price: 55.00
程序运行结果示例2:
输入:5,3.2 输出: Error in Area Price: 0.00
求各位大神指点,最好是解题思路 for i in range(10):
a=input('输入您的快递质量: ')
b=input('输入您的快递目的地(请输入同城,1,2,3,4区的其中一个): ')
a=float(a)
if a%1==0:
d=(a-1)//1
else:
d=(a-1)//1+1
c=(10 if b=='同城' and a<=1 else
10+3*d if b=='同城' and a>1 and a%1==0 else
10+3*d if b=='同城' and a>1 and a%1!=0 else
10 if b=='1区' and a<=1 else
10+4*d if b=='1区' and a>1 and a%1==0 else
10+4*d if b=='1区' and a>1 and a%1!=0 else
15 if b=='2区' and a<=1 else
15+5*d if b=='2区' and a>1 and a%1==0 else
15+5*d if b=='2区' and a>1 and a%1!=0 else
15 if b=='3区' and a<=1 else
15+6.5*d if b=='3区' and a>1 and a%1==0 else
15+6.5*d if b=='3区' and a>1 and a%1!=0 else
15 if b=='4区' and a<=1 else
15+10*d if b=='4区' and a>1 and a%1==0 else
15+10*d if b=='4区' and a>1 and a%1!=0 else
"请规范写入")
print(c)
这是我写的。我也是刚学,不知道有没有其他的问题。但是我验证过了,应该是可以解决问题的。
这好像问的是C语言的,我这个python好像不太适合。看错题了
#include<stdio.h>
float cost(int p,float w)
{
float qizong=15,xufei=0;
w=w<1?0:w-1;
w=w>(int)w?(int)w+1:(int)w;
switch(p)
{
case 0:
qizong=10;
xufei=3;
break;
case 1:
qizong=10;
xufei=4;
break;
case 2:
xufei =5;
break;
case 3:
xufei=6.5;
break;
case 4:
xufei=10;
break;
default:
printf("Error in Area ");
return 0;
}
return qizong+xufei*w;
}
int main()
{
int p;
float w;
scanf("%d,%f",&p,&w);
printf("Price:%.2f",cost(p,w));
return 0;
} C++#include <iostream>
#include <utility>
#include <vector>
#include <iomanip>
/*
(1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;
(2) 同城起重资费10元,续重3元/公斤;
(3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;
(4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:
2区的续重5元/公斤,
3区的续重6.5元/公斤,
4区的续重10元/公斤。
*/
using std::vector, std::pair;
vector <pair <float, float>> price{ {10, 3}, {10, 4}, {15, 5}, {15, 6.5}, {15, 10} };
float total(int area, float weight) {
float res = 0;
auto = price;
res += a;
weight -= 1;
while (weight > 0)
{
res += b;
weight -= 1;
}
return res;
}
using std::cout, std::cin, std::endl;
using std::fixed, std::setprecision;
int main(void) {
int area;
float weight;
char c;
cin >> area >> c >> weight;
if ((area < 0) or (area > 4)) {
cout << "Error in Area" << endl << "Price: 0.00" << endl;
}
else {
cout
<< fixed << setprecision(2)
<< "Price: "
<< total(area, weight)
<< endl;
}
return 0;
}
页:
[1]