题目:
要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高。
Smple input:161 189 167 172 188 Sample outPut: 188 189
分析:我的理解就是先逆序排好数值, 然后逐对比较身高差值, 找出身高差值最小的然后输出
代码如下:
package com.wenj.test; /** * 要从5个人中选取2个人作为礼仪,其中每个人的身高范围为160-190,要求2个人的身高差值最小(如果差值相同的话,选取其中最高的两人),以升序输出两个人的身高。 * 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