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]);
}
}