{ }codelper
最大值/最小值比较P0031

第二大的数

第一行给出整数个数 n,第二行给出 n 个整数。
输出不同的值中第二大的值。
同一个值出现多次只算一次(5 5 3 的答案是 3)。
如果不同的值只有一个,输出 "无"。

输入
1 ≤ n ≤ 100,-1000 ≤ 每个整数 ≤ 1000
输出
第二大的值,或 "无"
样例 1 · stdin
5
2 9 9 9 1
stdout
2
样例 2 · stdin
3
4 4 4
stdout
无
main.py
输入(stdin)

运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。

$ 就绪

请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。

其他语言怎么写?查看各语言题解 · 8 种语言题解

最常见的陷阱是最大值重复:排序后取倒数第二个(sorted(a)[-2]),5 5 3 会得到 5。
第二个陷阱是初始值:从 0 开始,全是负数时就错了,所以要从第一个值(C 中可用 INT_MIN)开始。
去掉重复的方法:Python 只留下比最大值小的再取 max,C# 用 Distinct().OrderByDescending(),C++ 放进 std::set 再取 std::next(s.rbegin())。
C 和 Java 遍历一次,同时更新最大值和第二大值。
Craykon 先用 遍历 (x 在 a) 找最大值,再遍历一次找 x < top 中最大的。

Pythonmain.py · 5 行
n = int(input())a = list(map(int, input().split()))top = max(a)rest = [x for x in a if x < top]print(max(rest) if rest else "无")
JavaScriptmain.js · 4 行
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const top = Math.max(...a);const rest = a.filter((x) => x < top);console.log(rest.length > 0 ? String(Math.max(...rest)) : "无");
TypeScriptmain.ts · 5 行
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const top: number = Math.max(...a);const rest: number[] = a.filter((x) => x < top);console.log(rest.length > 0 ? String(Math.max(...rest)) : "无");
JavaMain.java · 23 行
import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int first = sc.nextInt();        int second = 0;        boolean hasSecond = false;        for (int i = 1; i < n; i++) {            int x = sc.nextInt();            if (x > first) {                second = first;                hasSecond = true;                first = x;            } else if (x < first && (!hasSecond || x > second)) {                second = x;                hasSecond = true;            }        }        System.out.println(hasSecond ? String.valueOf(second) : "无");    }}
C#Main.cs · 17 行
using System;using System.Linq; class Program{    static void Main()    {        Console.ReadLine();        var distinct = Console.ReadLine()            .Split(' ', StringSplitOptions.RemoveEmptyEntries)            .Select(int.Parse)            .Distinct()            .OrderByDescending(x => x)            .ToList();        Console.WriteLine(distinct.Count >= 2 ? distinct[1].ToString() : "无");    }}
C++main.cpp · 15 行
#include <iostream>#include <iterator>#include <set> int main() {    int n, x;    std::cin >> n;    std::set<int> values;    for (int i = 0; i < n; i++) {        std::cin >> x;        values.insert(x);    }    if (values.size() < 2) std::cout << "无\n";    else std::cout << *std::next(values.rbegin()) << "\n";}
Cmain.c · 20 行
#include <stdio.h> int main(void) {    int n, x, first, second = 0, has_second = 0;    scanf("%d %d", &n, &first);    for (int i = 1; i < n; i++) {        scanf("%d", &x);        if (x > first) {            second = first;            has_second = 1;            first = x;        } else if (x < first && (!has_second || x > second)) {            second = x;            has_second = 1;        }    }    if (has_second) printf("%d\n", second);    else puts("无");    return 0;}
Craykonmain.cray · 25 行
n = 控制台.读行().转数字();a = 控制台.读数字();top = a[0];遍历 (x 在 a){    top = 数学.较大值(top, x);}found = 假;second = 0;遍历 (x 在 a){    如果 (x < top && (!found || x > second))    {        second = x;        found = 真;    }}如果 (found){    控制台.输出(second);}否则{    控制台.输出("无");}