Huawei hands-on exercises -- identify the best two-person group by height

Source: Internet
Author: User
Tags ranges

Question:

Select two people from five as etiquette. The height of each person ranges from 160 to 190, and the height difference between two people is the minimum (if the difference is the same, select the highest two persons), and output the height of the two persons in ascending order.

Smple input: 161 189 167 172 Sample outPut: 188 188


Analysis: My understanding is to first sort the values in reverse order, then compare the height difference one by one, find the minimum height difference, and then output


The Code is as follows:

Package com. wenj. test;
/**
* Two of the five should be selected as etiquette. The height of each person ranges from 160 to 190, and the height difference between the two must be the minimum (if the difference is the same, select the highest two persons), and output the height of the two persons in ascending order.
* Smple input: 161 189 167 172 188 Sample outPut: 188 189
* @ Author wenj91-PC
*
*/

Public class TestBestGround {

Public static void main (String args []) {
String strIn = "161 189 167 172 188 ";
TestBestGround tb = new TestBestGround ();
Tb. printTheBestGround (strIn );
}

Public void printTheBestGround (String strIn ){
String strTemp = strIn;
String [] strArr = strTemp. split ("");

Int [] numArr = new int [strArr. length];
For (int I = 0; I <strArr. length; I ++ ){
NumArr [I] = Integer. parseInt (strArr [I]);
}

For (int I = 0; I <numArr. length; I ++ ){
For (int j = I + 1; j <numArr. length; j ++ ){
If (numArr [I] <numArr [j]) {
Int temp = numArr [I];
NumArr [I] = numArr [j];
NumArr [j] = temp;
}
}
}

Int aver = numArr [0]-numArr [1];
Int pos = 0;

For (int I = 1; I <numArr. length-1; I ++ ){
Int temp = numArr [I]-numArr [I + 1];
If (temp <aver ){
Aver = temp;
Pos = I;
}
}

System. out. println (numArr [pos + 1] + "" + numArr [pos]);

}
}


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.