酷代码 AI
菜单
服务商

C++ 实现:从先增后减序列中精准找出最高礁石编号及代码详解

```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; int heights[10]; // 读取每块礁石的高度 for (int i = 0; i < n; i++) { cin >> heights[i]; } int maxIndex = 0; // 找出高度最大的礁石的编号 for (int i = 1; i < n; i++) { if (heights[i] > heights[maxIndex]) { maxIndex = i; } } cout << maxIndex << endl; return 0; } ``` 代码解释 1. 输入部分: - 首先读取一个整数 `n`,代表礁石的数量。 - 接着使用一个循环读取 `n` 个整数,存储在数组 `heights` 中,这些整数表示每块礁石的高度。 2. 查找最高礁石编号: - 初始化 `maxIndex` 为 0,假设第一块礁石是最高的。 - 遍历数组 `heights`,如果当前礁石的高度大于 `maxIndex` 对应的礁石高度,就更新 `maxIndex` 为当前礁石的编号。 3. 输出结果: - 输出 `maxIndex`,即最高礁石的编号。 ######[AI问答 | 318点数解答 | 2025-12-06 19:48:35]

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