Written questions: Number of minimum difference after sorting

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.