Blue Bridge cup of java training algorithm to find the maximum value in the array

Training algorithm to find the maximum value in the array

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

For a given integer array A [], where to find the maximum value, and returns the index.

Input Format

Integer array a [], an equal number of array elements is less than 100. The output data is divided into two lines: the first is only a number representing the number of array elements; behavior of each element of the second array.

Output Format

Outputting value, and subscript

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] list = new int[n];
		int maxIndex = 0;
		for (int i = 0; i < n; i++) {
			list[i] = in.nextInt();
			if (list[i] > list[maxIndex]) {
				maxIndex = i;
			}
		}
		System.out.println(list[maxIndex] + " " + maxIndex);
	}
 
}
Published 24 original articles · won praise 2 · Views 173

Guess you like

Origin blog.csdn.net/weixin_44570019/article/details/104515832