두 번째로 큰 수
첫 줄에 정수의 개수 n, 둘째 줄에 정수 n개가 주어진다.
서로 다른 값들 가운데 두 번째로 큰 값을 출력하라.
같은 값이 여러 번 나와도 한 번으로 친다(5 5 3이면 답은 3).
서로 다른 값이 하나뿐이면 "없음"을 출력하라.
- 입력
- 1 ≤ n ≤ 100, -1000 ≤ 각 정수 ≤ 1000
- 출력
- 두 번째로 큰 값, 또는 "없음"
5 2 9 9 9 1
2
3 4 4 4
없음
첫 줄에 정수의 개수 n, 둘째 줄에 정수 n개가 주어진다.
서로 다른 값들 가운데 두 번째로 큰 값을 출력하라.
같은 값이 여러 번 나와도 한 번으로 친다(5 5 3이면 답은 3).
서로 다른 값이 하나뿐이면 "없음"을 출력하라.
5 2 9 9 9 1
2
3 4 4 4
없음
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
가장 흔한 함정은 가장 큰 값이 겹칠 때예요.
정렬해서 뒤에서 둘째(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는 한 번 돌며 큰 값·둘째 값을 같이 고쳐 가요.
크레이콘은 각각 (x 에서 a)로 가장 큰 값을 찾고, 한 번 더 돌며 x < top인 것 중 가장 큰 것을 찾아요.
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 "없음")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)) : "없음");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)) : "없음");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) : "없음"); }}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() : "없음"); }}#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";}#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;}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);}아니면{ 콘솔.쓰기("없음");}