日期:2014-05-20 浏览次数:20819 次
import java.util.*; import java.io.*; public class job { private static double ipair(double d[], int n, int m) { if (n == m) { return Double.MAX_VALUE; } else if (n == m - 1) { return Math.abs(d[m] - d[n]); } else { double mid; double d1[] = new double[d.length]; double d2[] = new double[d.length]; mid = findMax(d) / 2 + findMin(d) / 2; int i, j = 0, k = 0; for (i = n; i <= m; i++) { if (d[i] < mid) { d1[j] = d[i]; ++j; } else { d2[k] = d[i]; ++k; } } double x1 = ipair(d1, 0, j - 1); double x2 = ipair(d2, 0, k - 1); double x3 = findMin(d2) - findMax(d1); return Math.min(x3, Math.min(x1, x2)); } } private static double findMax(double d[]) { double min = Double.MIN_VALUE; for (int i = 0; i < d.length; i++) if (min < d[i]) min = d[i]; return min; } private static double findMin(double d[]) { double max = Double.MAX_VALUE; for (int i = 0; i < d.length; i++) if (max > d[i]) max = d[i]; return max; } public static void main(String args[]) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); // 读取字符串 String str[] = s.split(" "); double dou[] = new double[str.length]; for (int i = 0; i < str.length; i++) { dou[i] = Double.parseDouble(str[i]); } double ss = ipair(dou, 0, dou.length - 1); System.out.println("最接近距离 " + ss); } }
public class readtest { private static double ipair(double d[], int n, int m) { if (n == m) { return Double.MAX_VALUE; } else if (n == m - 1) { return Math.abs(d[m] - d[n]); } else { double mid; double d1[] = new double[d.length]; double d2[] = new double[d.length]; mid = findMax(d, n, m) / 2 + findMin(d, n, m) / 2; int i, j = 0, k = 0; for (i = n; i <= m; i++) { if (d[i] < mid) { d1[j] = d[i]; ++j; } else { d2[k] = d[i]; ++k; } } double x1 = ipair(d1, 0, j - 1); double x2 = ipair(d2, 0, k - 1); double x3 = findMin(d2, 0, k - 1) - findMax(d1, 0, j - 1); return Math.min(x3, Math.min(x1, x2)); } } private static double findMax(double d[], int n, int m) { double min = Double.MIN_VALUE; for (int i = n; i <= m; i++) if (min < d[i]) min = d[i]; return min; } private static double findMin(double d[], int n, int m) { double max = Double.MAX_VALUE; for (int i = n; i <= m; i++) if (max > d[i]) max = d[i]; return max; } public static void main(String args[]) { Scanner scan = new Scanner(System.in); String s = scan.nextLine(); // 读取字符串 String str[] = s.split(" "); double dou[] = new double[str.length]; for (int i = 0; i < str.length; i++) { dou[i] = Double.parseDouble(str[i]); } double ss = ipair(dou, 0, dou.length - 1); System.out.println("最接近距离 " + ss); } }