public class JinZhi { static class Model implements Comparable<Model> { int a; int b; int min;//a、b两个人的身高差值 public Model(int a, int b, int min) { this.a=a; this.b=b; this.min=min; } @Override public int compareTo(Model o) { // TODO Auto-generated method stub if (this.min<o.min) { return -1; } else { return 1; } } @Override public String toString() { // TODO Auto-generated method stub return a+" "+b+" "+min; } } public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int[] a=new int[5]; int i=0; while(i<5&&scanner.hasNextInt()) { a[i++]=scanner.nextInt(); } TreeSet<Model> treeSet=new TreeSet<>(); for (int j = 0; j < a.length; j++) { for (int k = j+1; k < a.length; k++) { treeSet.add(new Model(j, k, Math.abs(a[j]-a[k]))); } } Iterator<Model> iterator=treeSet.iterator(); int max_1=0; int max_2=0; int min=0; while(iterator.hasNext()) { Model tempModel=iterator.next(); if (min==0) { if (a[tempModel.a]>max_1) { max_2=max_1; max_1=a[tempModel.a]; } else if (a[tempModel.a]<max_1&&a[tempModel.a]>max_2){ max_2=a[tempModel.a]; } if (a[tempModel.b]>max_1) { max_2=max_1; max_1=a[tempModel.b]; } else if (a[tempModel.b]<max_1&&a[tempModel.b]>max_2){ max_2=a[tempModel.b]; } min=tempModel.min; } else if(tempModel.min==min) { if (a[tempModel.a]>max_1) { max_2=max_1; max_1=a[tempModel.a]; } else if (a[tempModel.a]<max_1&&a[tempModel.a]>max_2){ max_2=a[tempModel.a]; } if (a[tempModel.b]>max_1) { max_2=max_1; max_1=a[tempModel.b]; } else if (a[tempModel.b]<max_1&&a[tempModel.b]>max_2){ max_2=a[tempModel.b]; } } else break; } System.out.println(max_2+" "+max_1); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lyric_315/article/details/47727153