酷代码 AI
菜单
服务商

用 C++ 程序计算船上需扔下超重物品的数量与总重

```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; int weights[10]; for (int i = 0; i < n; i++) { cin >> weights[i]; } int x; cin >> x; int count = 0; int totalWeight = 0; for (int i = 0; i < n; i++) { if (weights[i] > x) { count++; totalWeight += weights[i]; } } cout << count << " " << totalWeight; return 0; } ``` 代码解释: 1. 输入物品数量:使用`cin`读取物品的数量`n`。 2. 输入每件物品的重量:定义一个长度为10的数组`weights`,使用`for`循环读取`n`件物品的重量并存储在数组中。 3. 输入重量阈值:使用`cin`读取重量阈值`x`。 4. 遍历物品重量数组:使用`for`循环遍历`weights`数组,对于每个重量,如果它大于`x`,则将计数器`count`加1,并将该重量累加到`totalWeight`中。 5. 输出结果:使用`cout`输出需要扔下船的物品的数量`count`和这些物品的重量总和`totalWeight`,中间用空格隔开。 ######[AI问答 | 339点数解答 | 2025-12-06 19:21:38]

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