酷代码 AI
菜单
服务商

C++ 实现在线商城商品总价及运费计算

以下是实现该功能的 C++ 代码: ```cpp #include <iostream> using namespace std; int main() { int n; // 读取商品数量 cin >> n; int totalPrice = 0; for (int i = 0; i < n; i++) { int price; // 读取每件商品的价格 cin >> price; // 累加商品价格 totalPrice += price; } // 判断是否需要支付运费 if (totalPrice < 300) { totalPrice += 10; } // 输出总支付金额 cout << totalPrice << endl; return 0; } ``` 代码解释: 1. 输入商品数量:使用 `cin` 读取用户输入的商品数量 `n`。 2. 计算商品总价:通过 `for` 循环读取每件商品的价格,并累加到 `totalPrice` 中。 3. 判断是否需要支付运费:如果 `totalPrice` 小于 300 元,则需要额外支付 10 元运费,将其加到 `totalPrice` 中。 4. 输出总支付金额:使用 `cout` 输出乐乐一共需要支付的金额。 ######[AI问答 | 315点数解答 | 2026-02-13 18:08:47]

相关提问
发财导航,免费问AI
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]