gpt4 book ai didi

Java递归 "find median"算法异常错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:28:13 25 4
gpt4 key购买 nike

给定两个具有唯一整数元素的数组,即元素在两个数组内或两个数组之间重复:

这是我编写的递归算法,需要帮助来破译我收到的错误消息。

public class Median {
public static void main(String[] args)
{
int[] arr1 = new int[]{1,2,3,4,5};
int[] arr2 = new int[]{6,7,8,9,10};
medianOfBoth(arr1, arr2);
}

public static int medianOfBoth(int[] arr1, int[] arr2)
{
if (arr1.length <= 2 && arr2.length <= 2)
{
return compute_median(arr1, arr2);
}

else if (median(arr1) > median(arr2))
{
//erase top half of arr1 and bottom half of arr2
arr1 = Arrays.copyOfRange(arr1, 0, median(arr1));
arr2 = Arrays.copyOfRange(arr2, median(arr2), arr2.length - 1);
return medianOfBoth(arr1, arr2);
}

else // if (median(arr1) < median(arr2))
{
arr1 = Arrays.copyOfRange(arr1, median(arr1), arr1.length - 1);
arr2 = Arrays.copyOfRange(arr2, 0, median(arr2));
return medianOfBoth(arr1,arr2);
}
}

public static int median(int[] arr)
{
return arr[arr.length/2];
}

public static int compute_median(int[] arr1, int[] arr2)
{
//arr1 and arr2 are either length 1 or 2 in this function
return (max(arr1[0], arr2[0]) + min(arr1[arr1.length-1], arr2[arr2.length-1])) / 2;
}

public static int max(int x, int y)
{
if (x>y)
return x;
else
return y;
}

public static int min(int x, int y)
{
if (x<y)
return x;
else
return y;
}
}

这是我收到的错误信息

Exception in thread "main" java.lang.IllegalArgumentException: 4 > 0
at java.util.Arrays.copyOfRange(Arrays.java:2621)
at Median.medianOfBoth(Median.java:28)
at Median.medianOfBoth(Median.java:30)
at Median.main(Median.java:8)

最佳答案

稍微更改了您的代码。请注意。

import java.io.*;

import java.util.*;

public class Main {

public static int median(int[] arr)

{

return arr[arr.length/2];
}

public static int med(int[] arr)
{
return arr.length/2;
}

public static int max(int x, int y)
{
if (x>y)
return x;
else
return y;
}

public static int min(int x, int y)
{
if (x<y)
return x;
else
return y;
}

public static int compute_median(int[] arr1, int[] arr2)
{
//arr1 and arr2 are either length 1 or 2 in this function
return (max(arr1[0], arr2[0]) + min(arr1[arr1.length-1], arr2[arr2.length-1])) / 2;
}

public static int medianOfBoth(int[] arr1, int[] arr2)
{
if (arr1.length <= 2 && arr2.length <= 2)
{
return compute_median(arr1, arr2);
}

else if (median(arr1) > median(arr2))
{
//erase top half of arr1 and bottom half of arr2
//System.out.println("hi ="+med(arr2)+" "+arr2.length+"\n");
arr1 = Arrays.copyOfRange(arr1, 0, med(arr1));
arr2 = Arrays.copyOfRange(arr2, med(arr2), arr2.length);
return medianOfBoth(arr1, arr2);
}

else // if (median(arr1) < median(arr2))
{
//System.out.println("bi ="+med(arr1)+" "+arr1.length+"\n");
arr1 = Arrays.copyOfRange(arr1, med(arr1), arr1.length);
arr2 = Arrays.copyOfRange(arr2, 0, med(arr2));
return medianOfBoth(arr1,arr2);
}
}

public static void main(String[] args)
{
int[] arr1 = new int[]{1,2,3,4,5};
int[] arr2 = new int[]{1,3,5,7,9};
System.out.println(medianOfBoth(arr1, arr2));
}
}

关于Java递归 "find median"算法异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618050/

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