鱼C论坛

 找回密码
 立即注册
查看: 3833|回复: 4

[已解决]一道作业题

[复制链接]
发表于 2022-12-3 14:28:31 | 显示全部楼层
C++
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4. #include <iomanip>

  5. /*
  6. (1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;
  7. (2) 同城起重资费10元,续重3元/公斤;
  8. (3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;
  9. (4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:
  10.                 2区的续重5元/公斤,
  11.                 3区的续重6.5元/公斤,
  12.                 4区的续重10元/公斤。
  13. */

  14. using std::vector, std::pair;
  15. vector <pair <float, float>> price{ {10, 3}, {10, 4}, {15, 5}, {15, 6.5}, {15, 10} };

  16. float total(int area, float weight) {
  17.         float res = 0;
  18.         auto [a, b] = price[area];
  19.         res += a;
  20.         weight -= 1;
  21.         while (weight > 0)
  22.         {
  23.                 res += b;
  24.                 weight -= 1;
  25.         }
  26.         return res;
  27. }

  28. using std::cout, std::cin, std::endl;
  29. using std::fixed, std::setprecision;
  30. int main(void) {
  31.         int area;
  32.         float weight;
  33.         char c;
  34.         cin >> area >> c >> weight;
  35.         if ((area < 0) or (area > 4)) {
  36.                 cout << "Error in Area" << endl << "Price: 0.00" << endl;
  37.         }
  38.         else {
  39.                 cout
  40.                         << fixed << setprecision(2)
  41.                         << "Price: "
  42.                         << total(area, weight)
  43.                         << endl;
  44.         }
  45.         return 0;
  46. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-4 15:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表