가장 큰 수와 위치
첫 줄에 n, 둘째 줄에 정수 n개가 주어진다.
가장 큰 수와, 그 수가 처음 나오는 위치(1부터 셈)를 공백으로 출력하라.
- 입력
- 1 ≤ n ≤ 1,000, -1,000,000 ≤ 각 수 ≤ 1,000,000
- 출력
- 최댓값 위치
5 3 9 2 9 1
9 2
첫 줄에 n, 둘째 줄에 정수 n개가 주어진다.
가장 큰 수와, 그 수가 처음 나오는 위치(1부터 셈)를 공백으로 출력하라.
5 3 9 2 9 1
9 2
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
같은 최댓값이 여러 번 나오면 처음 위치예요: 비교를 >로 해야 하고 >=로 하면 마지막 위치가 돼요.
Python은 max()와 index()로 두 번 훑고, 다른 언어는 한 번 훑으며 위치를 기억해요.
위치는 0부터 세는 번호에 1을 더해요.
n = int(input())a = list(map(int, input().split()))m = max(a)print(m, a.index(m) + 1)const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best = 0;for (let i = 1; i < n; i++) if (a[i] > a[best]) best = i;console.log(a[best], best + 1);import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best: number = 0;for (let i = 1; i < n; i++) if (a[i] > a[best]) best = i;console.log(a[best], best + 1);import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; System.out.println(a[best] + " " + (best + 1)); }}using System; class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; Console.WriteLine($"{a[best]} {best + 1}"); }}#include <algorithm>#include <iostream>#include <vector> int main() { int n; std::cin >> n; std::vector<int> a(n); for (int& x : a) std::cin >> x; int best = std::max_element(a.begin(), a.end()) - a.begin(); std::cout << a[best] << ' ' << best + 1 << '\n';}#include <stdio.h> int main(void) { int n, a[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int best = 0; for (int i = 1; i < n; i++) if (a[i] > a[best]) best = i; printf("%d %d\n", a[best], best + 1); return 0;}n = 콘솔.줄읽기().수로변환();a = 콘솔.수들읽기();best = 0;반복 (i = 1; i < n; i++){ 만약 (a[i] > a[best]) { best = i; }}콘솔.쓰기(a[best] + " " + (best + 1));