Written question: To choose 2 persons from 5 people as a courtesy, of which each person's height range is 160-190, 2 people are required to the minimum height difference (if the difference is the same, select the highest of the two), in order to output two people in ascending height. Smple input:161 189 167 172 188 Sample output:188 189
Public classdemo06{ Public Static voidMain (string[] args) {int[] arr={161,189,167,172,188};//Original Value//sort the original values in ascending orderGetsort (arr);//after sorting in ascending order, remove the minimum difference and record the minimum difference subscriptint[] arrdiff=New int[4];intMin=arr[1]-arr[0];intIndex=-1;intOutput1=0,output2=0; for(inti=0;i<4;i++){if(Getdifferent (Arr[i+1],arr[i]) <=min) Index=i;} System.out.println (The two students with the lowest height difference are sorted in ascending order: "+arr[index]+", "+arr[index+1]);}//sort the array in ascending order Public Static int[] Getsort (int[] arr) {intLength=arr.length; System.out.println (length); for(intj=0;j<length-1;j++){if(Getdifferent (arr[j],arr[j+1]) >0){inttemp=Arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}returnarr;}//22 Subtract, get the difference Public Static intGetdifferent (intAintb) {returnA-b;}}
Written questions: Number of minimum difference after sorting