这一题直接用暴力
遍历数组,寻找最大值和次大值
保存a[i]的下标输出
#include<bits/stdc++.h>
using namespace std;
long long a[110];
int main(){
int n;
cin >> n;
int a[n+1];
// 读取n个数组元素的值
for(int i = 1; i <= n; i++)
cin >> a[i];
// 输出数组元素的值
for(int i = 1; i <= n; i++)
cout << a[i] << " ";
// 初始化变量s和ss,用于记录数组中的最大值和次大值
long long s = 0, ss = 0;
long long y;
// 遍历数组,寻找最大值和次大值
for(int i = 1; i <= n; i++){
// 如果当前元素大于s,则更新s为当前元素的值
if(a[i] > s)
s = a[i];
// 如果当前元素小于s且大于ss,则更新ss为当前元素的值,并记录索引
else if(a[i] < s && a[i] > ss){
ss = a[i];
y = i;
}
// 其他情况下继续遍历
else
continue;
}
// 输出次大值的索引
cout << y << endl;
return 0;
}