gpt4 book ai didi

java - 将扫描仪类型更改为数字数组

转载 作者:行者123 更新时间:2023-12-02 01:41:34 24 4
gpt4 key购买 nike

我有以下代码,而不是使用扫描仪功能输入数字。我想用数组代替。我该怎么做呢?我需要帮助。提前致谢

public class salomon {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
int[] minHeap = new int[n];
int[] maxHeap = new int[n];
int minHeapSize = 0;
int maxHeapSize = 0;

float currentMedian = 0;

for (int a_i = 0; a_i < n; a_i++) {
a[a_i] = in.nextInt();
if (a[a_i] < currentMedian) {
maxHeap[maxHeapSize++] = a[a_i];
// making sure the max heap has maximum value at the top
if (maxHeap[maxHeapSize - 1] > maxHeap[0]) {
swap(maxHeap, maxHeapSize - 1, 0);
}
} else {
minHeap[minHeapSize++] = a[a_i];
// making sure the min heap has minimum value at the top
if (minHeap[minHeapSize - 1] < minHeap[0]) {
swap(minHeap, minHeapSize - 1, 0);
}
}

// if the difference is more than one
if (Math.abs(maxHeapSize - minHeapSize) > 1) {
if (maxHeapSize > minHeapSize) {
swap(maxHeap, maxHeapSize - 1, 0);
minHeap[minHeapSize++] = maxHeap[--maxHeapSize];
swap(minHeap, 0, minHeapSize - 1);
buildMaxHeap(maxHeap, maxHeapSize);
} else {
swap(minHeap, minHeapSize - 1, 0);
maxHeap[maxHeapSize++] = minHeap[--minHeapSize];
swap(maxHeap, 0, maxHeapSize - 1);
buildMinHeap(minHeap, minHeapSize);
}
}

// calculate the median
if (maxHeapSize == minHeapSize) {
currentMedian = (minHeap[0] + maxHeap[0]);
currentMedian = currentMedian / 2;
} else if (maxHeapSize > minHeapSize) {
currentMedian = maxHeap[0];
} else {
currentMedian = minHeap[0];
}

System.out.println(currentMedian);

}

}

static void buildMaxHeap(int[] input, int heapSize) {
int depth = (heapSize - 1) / 2;
for (int i = depth; i >= 0; i--) {
maxHeapify(input, i, heapSize);
}
}

static void maxHeapify(int[] input, int i, int heapSize) {
int left = 2 * i + 1;
int right = 2 * i + 2;

// find the largest
int largest = i;

if (left < heapSize && input[left] > input[largest]) {
largest = left;
}

if (right < heapSize && input[right] > input[largest]) {
largest = right;
}

if (largest != i) {
//swap
swap(input, i, largest);
//recursive call
maxHeapify(input, largest, heapSize);
}
}

static void buildMinHeap(int[] input, int heapSize) {
int depth = (heapSize - 1) / 2;
for (int i = depth; i >= 0; i--) {
minHeapify(input, i, heapSize);
}
}

static void minHeapify(int[] input, int i, int heapSize) {
int left = 2 * i + 1;
int right = 2 * i + 2;

// find the smallest
int smallest = i;

if (left < heapSize && input[left] < input[smallest]) {
smallest = left;
}

if (right < heapSize && input[right] < input[smallest]) {
smallest = right;
}

if (smallest != i) {
//swap
swap(input, i, smallest);
//recursive call
minHeapify(input, smallest, heapSize);
}
}

static void swap(int[] input, int i, int j) {
if (i == j)
return;
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}

当我输入 6, 12, 4, 5, 3, 8, 7 并输入 - 我得到移动中位数

12.0
8.0
5.0
4.5
5.0
6.0

我想用数组 {6, 12, 4, 5, 3, 8, 7}` 替换扫描仪相反以及如何调整代码。谢谢

最佳答案

第一个值6是数组长度。假设您打算保留这一点,您将删除所有“不是大象”的内容(即所有中位数计算),并将 int[] a 重命名为 int[] f 。就像,

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] f = new int[n];
for (int i = 0; i < n; i++) {
f[i] = in.nextInt();
}
System.out.println(Arrays.toString(f));
}

当使用示例输入执行时,会输出

[12, 4, 5, 3, 8, 7]

如果您确实也想要这六个,那么您需要添加它。就像,

int[] f = new int[n + 1];
f[0] = n;
for (int i = 1; i <= n; i++) {
f[i] = in.nextInt();
}
System.out.println(Arrays.toString(f));

输出(根据要求)

[6, 12, 4, 5, 3, 8, 7]

关于java - 将扫描仪类型更改为数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com