最大数及其位置
第一行给出 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()/标准输入传入(运行中不会等待输入)。
$ 就绪
请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。
最大值出现多次时取第一次的位置:比较要用 >,用 >= 会得到最后一次。
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));