使用 findKth 查找最低、最高、中位数和平均分数
Find Lowest, Highest, Median and Average Score using findKth
对于这个项目,我的目标是...
使用 findKth 找出最高分、最低分、中分和平均分
用户必须输入数字(输入 -1 以停止扫描仪)并且他们不知道有多少以及它们是否已排序
但是,我在尝试执行此操作时发现了一些问题。
我提供的 findKth 方法只接受一个 int[]arr,我找不到将数组初始化为该项目所需的特定大小的方法。
有人可以建议一种方法吗?
下面是我的测试方法和我的findKth
import java.util.*;
public class Statistics
{
public static void main(String[]args)
{
System.out.print("Enter Scores, -1 to end: ");
Scanner keyboard= new Scanner(System.in);
String numbers = null;
while(keyboard.nextInt()!=-1)
{
numbers= keyboard.next();
}
String[] parts = numbers.split(" ");
int[] n1 = new int[parts.length];
for(int n = 0; n < parts.length; n++)
{
n1[n] = Integer.parseInt(parts[n]);
}
int highest= n1.length-1;
int lowest=0;
int median= n1.length/2;
QuickSort.findKth(n1, highest);
System.out.println("High: "+n1[highest]);
QuickSort.findKth(n1, lowest);
System.out.println("Low: "+n1[lowest]);
QuickSort.findKth(n1, median);
System.out.println("Median: "+n1[median]);
}
}
public static void findKth(int[] arr, int k)
{
findKth(arr, 0, arr.length, k);
}
//Pre: arr[first]..arr[last-1] contain integers
// k must be in [first..last-1]
//Post: The elements in arr has been rearranged in such a way that arr[k] now contains the kth
// largest element
public static void findKth(int[] arr, int first, int last, int k)
{
int pivotLoc = rearrange(arr, first, last);
if (pivotLoc==k) return;
else if (pivotLoc>k) findKth(arr, first, pivotLoc, k);
else findKth (arr, pivotLoc +1, last, k);
}
我尝试了不同的方法,例如尝试解析数字字符串,但是我无法这样做,因为我找不到在用户输入 -1 时正确停止扫描仪的方法。
我也试过使用 ArrayList,但是 findKth 只需要一个 int[]arr。所以这行不通。
建议?我被难住了。
使用列表收集输入:
List<Integer> input = new ArrayList<>();
input.add(n); // add each number
然后全部输入后转为数组:
int[] array = input.stream().mapToInt(Integer::intValue).toArray();
您的输入循环有问题。虽然超出了问题的范围,但请尝试更简单的循环,例如:
while (true) {
int n = keyboard.nextInt();
if (n == -1)
break;
input.add(n);
}
对于这个项目,我的目标是... 使用 findKth 找出最高分、最低分、中分和平均分 用户必须输入数字(输入 -1 以停止扫描仪)并且他们不知道有多少以及它们是否已排序 但是,我在尝试执行此操作时发现了一些问题。
我提供的 findKth 方法只接受一个 int[]arr,我找不到将数组初始化为该项目所需的特定大小的方法。
有人可以建议一种方法吗?
下面是我的测试方法和我的findKth
import java.util.*;
public class Statistics
{
public static void main(String[]args)
{
System.out.print("Enter Scores, -1 to end: ");
Scanner keyboard= new Scanner(System.in);
String numbers = null;
while(keyboard.nextInt()!=-1)
{
numbers= keyboard.next();
}
String[] parts = numbers.split(" ");
int[] n1 = new int[parts.length];
for(int n = 0; n < parts.length; n++)
{
n1[n] = Integer.parseInt(parts[n]);
}
int highest= n1.length-1;
int lowest=0;
int median= n1.length/2;
QuickSort.findKth(n1, highest);
System.out.println("High: "+n1[highest]);
QuickSort.findKth(n1, lowest);
System.out.println("Low: "+n1[lowest]);
QuickSort.findKth(n1, median);
System.out.println("Median: "+n1[median]);
}
}
public static void findKth(int[] arr, int k)
{
findKth(arr, 0, arr.length, k);
}
//Pre: arr[first]..arr[last-1] contain integers
// k must be in [first..last-1]
//Post: The elements in arr has been rearranged in such a way that arr[k] now contains the kth
// largest element
public static void findKth(int[] arr, int first, int last, int k)
{
int pivotLoc = rearrange(arr, first, last);
if (pivotLoc==k) return;
else if (pivotLoc>k) findKth(arr, first, pivotLoc, k);
else findKth (arr, pivotLoc +1, last, k);
}
我尝试了不同的方法,例如尝试解析数字字符串,但是我无法这样做,因为我找不到在用户输入 -1 时正确停止扫描仪的方法。
我也试过使用 ArrayList,但是 findKth 只需要一个 int[]arr。所以这行不通。
建议?我被难住了。
使用列表收集输入:
List<Integer> input = new ArrayList<>();
input.add(n); // add each number
然后全部输入后转为数组:
int[] array = input.stream().mapToInt(Integer::intValue).toArray();
您的输入循环有问题。虽然超出了问题的范围,但请尝试更简单的循环,例如:
while (true) {
int n = keyboard.nextInt();
if (n == -1)
break;
input.add(n);
}