Java教程

22222222222

本文主要是介绍22222222222,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

`package Test;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int midX = 0;
int midY = 0;
int[] x = new int[n];
int[] y = new int[n];
for (int i=0; i<n; i++){
x[i] = in.nextInt();
y[i] = in.nextInt();
}
quick_Sort(x, 0, n-1);
quick_Sort(y, 0, n-1);
midX = x[n/2];
midY = y[n/2];
System.out.println("超市的位置为:("+midX+","+midY+")");
int sum = 0;
for (int i = 0; i < n; i++) {
sum += Math.abs(x[i] - midX) + Math.abs(y[i] - midY);
}
System.out.println("距离和为:"+sum);

}

//快速排序(挖坑法)
public static void quick_Sort(int a[], int left, int right){
    if (left >= right){
        return;
    }
    int i = left;
    int j = right;
    int base = a[left];
    while (i < j){
        while (i < j && a[j] >= base){
            j--;
        }
        if (i < j){
            a[i++] = a[j];
        }

        while (i <j && a[i] <= base){
            i++;
        }
        if (i < j){
            a[j--] = a[i];
        }

    }
    a[i] = base;
    quick_Sort(a, left, i-1);
    quick_Sort(a, i+1, right);
}

}

`

这篇关于22222222222的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!