找出身高差最小的两个人
/**
* @输入一组身高在170到190之间(5个身高),比较身高差,选出身高差最小的两个身高;若身高差相同,
* 选平均身高高的那两个身高;从小到大输出; 如输入 170 181 173 186 190输出 170 173
*/
public class MinDataHeight
{
public static void main(String[] args)
{
int a[]={170 ,181 ,173, 186 ,190};
System.out.println(gettarget(a));
}
public static String gettarget(int[] a)
{
int lenOfa = a.length;
int index1 = 0;//第一个人的指针
int index2 = 0;//第二个人的指针
int min =1000;
int meanheight=1000;
for(int i = 0;i < lenOfa; i++)
{
for (int j = 0; j < i; j++)
{
int dataHeight;
if (a[i] > a[j])
dataHeight = a[i] - a[j];
else
dataHeight = a[j] - a[i];
if (dataHeight < min ||
(min ==dataHeight && (a[i]+a[j])/2 > meanheight))
{
min =dataHeight ;
index1 = i;
index2 = j;
meanheight = (a[i]+a[j])/2;
}
}
}
int tall =a[index1] > a[index2] ? index1:index2;
int little =a[index1] <= a[index2] ? index1:index2;
String result=a[little] + " " + a[tall];
return result;
}
}