酷代码 AI
菜单
服务商

根据倍数分类计算箱子重量总和的程序实现

以下是使用 Python 实现的代码: ```python # 读取箱子的数量 n = int(input()) # 读取每个箱子的重量 weights = list(map(int, input().split())) # 读取倍数 x x = int(input()) # 初始化总和为 0 total_weight = 0 # 遍历每个箱子的重量 for weight in weights: # 判断箱子重量是否是 x 的倍数 if weight % x == 0: # 如果是,则累加到总和中 total_weight = total_weight + weight # 输出放在船尾的箱子的重量总和 print(total_weight) ``` 以下是使用 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; // 读取倍数 x cin >> x; int total_weight = 0; // 遍历每个箱子的重量 for (int i = 0; i < n; i++) { // 判断箱子重量是否是 x 的倍数 if (weights[i] % x == 0) { // 如果是,则累加到总和中 total_weight += weights[i]; } } // 输出放在船尾的箱子的重量总和 cout << total_weight << endl; return 0; } ``` ######[AI问答 | 386点数解答 | 2026-03-27 19:46:41]

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