gpt4 book ai didi

java - 关于CodeChef上的 "Uncle Johnny"问题_

转载 作者:行者123 更新时间:2023-12-01 21:54:41 25 4
gpt4 key购买 nike

有一道练习题Uncle Johnywww.codechef.com

由于篇幅较长,我提供了链接。

https://www.codechef.com/problems/JOHNY/

我有两个解决该问题的方法(代码 1 和代码 2)

代码1

class UncleJohny
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);

int test_case = Integer.parseInt(br.readLine());

while(test_case-- > 0)
{
int n = Integer.parseInt(br.readLine());

int i = 0;

String a[] = br.readLine().split(" "); //Mind this line

int k = Integer.parseInt(br.readLine());

String temp = a[k - 1];

Arrays.sort(a);

pw.println(Arrays.binarySearch(a, temp) + 1);
}

pw.flush();
}
}

代码2

class UncleJohny
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);

int test_case = Integer.parseInt(br.readLine());

while(test_case-- > 0)
{
int n = Integer.parseInt(br.readLine());

int a[] = new int[n];

int i = 0;

for(String str: br.readLine().split(" "))
{
a[i++] = Integer.parseInt(str); //Mind this line
}

int k = Integer.parseInt(br.readLine());

int temp = a[k - 1];

Arrays.sort(a);

pw.println(Arrays.binarySearch(a, temp) + 1);
}

pw.flush();
}
}

上述代码的基本任务是找到temp值的索引。在输入数组 a 中排序后

据我所知,这两个代码的输出不会有任何差异。 (如有错误请指正)

CodeChef 正在接受 Code 2 ,但对 Code 1错误答案

我的查询到底是什么?

尽管相同,为什么 code 2被接受,而 code 1不是吗?

为什么我需要将输入值存储在 int 中数组(如代码 2 所示)而不是将它们存储到 String 中数组(如代码1所示),为了让我的答案被接受?

最佳答案

整数的排序顺序与字符串的排序顺序不同。例如。 “1”<“10”<“2”...

关于java - 关于CodeChef上的 "Uncle Johnny"问题_,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545299/

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