思路及代码
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int n;
while (cin >> n) {
int list[n];
for (int i = 0; i < n; i++) {
cin >> list[i];
}
// solution
int max = list[0];
int sec_max = list[0];
if (n == 0 || n == 1){
cout << "none" << endl;
continue;
}
// normal case
for (int i = 1; i < n; i++) {
// find max
if (list[i] > max) {
max = list[i];
}
// find sec max
if (list[i] < max && list[i] > sec_max) {
sec_max = list[i];
}
}
// edge case: list.size = 0 or all members are equal in list
if (max == sec_max) {
cout << "none" << endl;
}else{
cout << sec_max;
for (int i = 0; i < n; i++) {
if (list[i] == sec_max) {
cout << " " << i;
}
}
cout << endl;
}
}
}
收获:1️⃣先写清楚输入,再写 solution
2️⃣复习数组的排序sort(...下标1,...下标2)
菜菜,不是教程,做题和学习记录