package java程序员面试笔试宝典;
public class 题8_5_19最小三元组距离 {
public static void main(String[] args) {
int[] a={3,4,5,7};
int[] b={10,12,14,16,17};
int[] c={20,21,23,24,27,30};
System.out.println(minDis_0(a, b, c));
}
//最小距离法:每次只考虑三个元素中,哪个元素最小,
//最小元素所在的数组元素递增,可以更新得到结果
public static int minDis_0(int[] a,int[] b,int[] c){
int i=0;
int j=0;
int k=0;
int cur=Integer.MAX_VALUE;
while(true){
int temp=max(Math.abs(a[i]-b[j]),
Math.abs(a[i]-c[k]), Math.abs(b[j]-c[k]));
if(temp<cur){
cur=temp;
}
int min=min(a[i], b[j], c[k]);
if(min==a[i]){
i++;
if(i>a.length-1){
break;
}
}
if(min==b[j]){
j++;
if(j>b.length-1){
break;
}
}
if(min==c[k]){
k++;
if(k>c.length-1){
break;
}
}
}
return cur;
}
public static int minDis(int[] a,int[] b,int[] c){
int min;
int minDis=max(Math.abs(a[0]-b[0]),
Math.abs(a[0]-c[0]), Math.abs(b[0]-c[0]));
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
for (int k = 0; k < c.length; k++) {
min=max(Math.abs(a[i]-b[j]),
Math.abs(a[i]-c[k]), Math.abs(b[j]-c[k]));
if(min<minDis){
minDis=min;
}
}
}
}
//System.out.println(minDis);
return minDis;
}
public static int max(int a,int b,int c){
int max=a>b?a:b;
max=max>c?max:c;
return max;
}
public static int min(int a,int b,int c){
int min=a<=b?a:b;
min=min<=c?min:c;
return min;
}
}