3264 - 例题4-10 运费计算
时间限制 : 1 秒
内存限制 : 12 MB
运输公司对用户计算运输费用。路程(km)越远(以s表示),每吨.千米的运费越低。
计算标准如下:

设基本运费为p,货物重量为w,运输距离为s,折扣率为d,则总运费f的计算公式如下:
题目输入
输入运输单价、重量和距离,以空格分隔。
数据类型定义为float类型。
题目输出
freight=计算出的总运费。
小数点后保留2位数字,末尾输出换行。
输入/输出样例
输入格式
100 20 300
输出格式
freight=588000.00
C语言解答
#include<stdio.h> int main() { float p,w,s,d,f; scanf("%f%f%f",&p,&w,&s); if(s<250) d=0; if(s>=250&&s<500) d=0.02; if(s>=500&&s<1000) d=0.05; if(s>=1000&&s<2000) d=0.08; if(s>=2000&&s<3000) d=0.1; if(s>=3000) d=0.15; f=p*w*s*(1-d); printf("freight=%.2f\n",f); getchar(); getchar(); return 0; }
C++解答
#include<iostream> #include<cstdio> using namespace std; int main() { float p,w,s,d,f; int c; cin>>p>>w>>s; if (s>=3000) c=12; else c=int(s/250); switch (c) { case 0: d=0; break; case 1: d=2; break; case 2: case 3: d=5; break; case 4: case 5: case 6: case 7: d=8; break; case 8: case 9: case 10: case 11: d=10; break; case 12: d=15; break; } f=p*w*s*(1-d/100); printf("freight=%.2f\n",f); return 0; }