酷代码 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; return 0; } ``` 代码解释 1. 输入礁石数量:使用`cin`读取一个整数`n`,表示礁石的数量。 2. 输入每块礁石的高度:定义一个长度为10的数组`heights`,使用`for`循环读取`n`个整数,依次存储到数组中。 3. 寻找最高礁石的编号:初始化`maxIndex`为0,表示最高礁石的编号初始为0。使用`for`循环遍历数组,若当前元素比`heights[maxIndex]`大,则更新`maxIndex`为当前元素的索引。 4. 输出结果:输出`maxIndex`,即最高礁石的编号。 ######[AI问答 | 288点数解答 | 2025-11-08 19:17:37]

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