【问题标题】:regarding "Uncle Johnny" problem_ on CodeChef关于 CodeChef 上的“约翰尼叔叔”问题_
【发布时间】: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 1Wrong Answer

我的查询到底是什么?

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

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

【问题讨论】:

    标签: java arrays string int binary-search


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-23
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多