酷代码 AI
菜单
服务商

寻找先递增后递减序列中最大值索引的C++实现

以下是使用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`,然后使用循环读取每个礁石的高度并存储在数组`heights`中。 2. **寻找最高礁石**:使用一个变量`maxIndex`来记录当前最高礁石的编号,初始化为0。然后遍历数组,如果当前元素比`heights[maxIndex]`大,则更新`maxIndex`为当前元素的索引。 3. **输出结果**:最后输出`maxIndex`,即最高礁石的编号。 ######[AI问答 | 317点数解答 | 2026-03-27 20:14:37]

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