gpt4 book ai didi

java - 附加输入

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:10 24 4
gpt4 key购买 nike

我正在解决Kattis问题。我不确定如何获取第二组输入数据以进行识别和处理。有人可以提供协助吗?

详细信息如下:

指令:


  输入:输入的第一行给出测试用例的数量。
  每个测试用例有两行。第一个给出
  商店,第二个给出他们在Long上的整数位置
  街。输出:为每个测试用例输出一行
  最短步行距离,可提供最佳停车位。


输入和输出示例:

输入值
2
4
24
13
89
37
6
7 30 41 14 39 42

输出:
152
70

因此,基本上,您需要将最大/最小int之差加倍。我已经获得了第一组数据(24、13、89、37)来计算152,但是我的程序忽略了第二组数据(7、30、41、14、39、42)。这是代码:

    public static void main(String[] args) {
//create scanner for input
Scanner in = new Scanner (System.in);

//create variables for first two input lines
int cases = in.nextInt();
int numOfStores = in.nextInt();

//initialize array
int numArray [] = new int[numOfStores];

//assign values to array
for (int i = 0; i < numArray.length; i++){
numArray[i] = in.nextInt();}

//sort array
Arrays.sort(numArray);

//set min value to first index position of array
int min = numArray[0];

//set max value to last index position of array
int max = numArray[numArray.length - 1];

//print out the difference of max and min multiplied by 2 for distance to and from
System.out.println((max - min) * 2);

最佳答案

您需要循环输入案例的次数。
因此,您可以使用for循环。

int cases = in.nextInt();
for(int c = 0; c < cases; c++){
//rest of the code
}

关于java - 附加输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675473/

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