【发布时间】:2015-12-31 10:51:36
【问题描述】:
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();
}
}
上述代码中的基本任务是排序后在输入数组a中找到temp的值的索引
据我所知,这两个代码的输出不会有任何差异。 (如果我错了,请纠正我)
CodeChef 接受 Code 2,但对 Code 1 说 Wrong Answer
我的查询到底是什么?
尽管相同,为什么code 2 被接受而code 1 不被接受?
为什么我需要将输入值存储在int数组中(如代码2所示)而不是将它们存储在String数组中(如代码1所示),以使我的答案被接受?
【问题讨论】:
标签: java arrays string int binary-search