蓝桥杯 java 算法训练 寻找数组中最大值

算法训练 寻找数组中最大值

资源限制

时间限制:1.0s 内存限制:512.0MB

问题描述

对于给定整数数组a[],寻找其中最大值,并返回下标。

输入格式

整数数组a[],数组元素个数小于1等于100。输出数据分作两行:第一行只有一个数,表示数组元素个数;第二行为数组的各个元素。

输出格式

输出最大值,及其下标

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);
	}
 
}
发布了24 篇原创文章 · 获赞 2 · 访问量 173

猜你喜欢

转载自blog.csdn.net/weixin_44570019/article/details/104515832
今日推荐