java(结构体排序)

问题描述

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

输入格式

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

输出格式

  输出最大值,及其下标

样例输入

3
3 2 1

样例输出

3 0

我觉得,题目有点小错误,但不影响读题,就是找出最大值及其下标,注意下标从0开始。

import java.util.*;
public class Main {
	static Scanner cin = new Scanner(System.in);
	static class hh{
		int v;
		int id;
		public hh(int v,int id) {
			this.v=v;
			this.id=id;	
		}
	}//结构体
	static hh [] a = new hh[5000];
	static Comparator cmp = new Comparator <hh>() {
		public int compare(hh a,hh b){
			return a.v-b.v;
		}
	};//结构体比较函数
	public static void main(String[] args){
		int n = cin.nextInt();
		for (int i = 0; i < n;i++) {
			a[i]=new hh(cin.nextInt(),i);//输入方式
		}
		Arrays.sort(a, 0,n,cmp);
		System.out.println(a[n-1].v + " " + a[n-1].id);
	}
}

猜你喜欢

转载自blog.csdn.net/lgz0921/article/details/82345775
今日推荐