在 Java 中获取输入最有效的方式是什么?

发布于 10-17 00:22 字数 1473 浏览 6 评论 0原文

我正在解决这个问题

这是我的代码:

import java.io.IOException;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] t = new int[n];
        int count = 0;
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            if (t[i] % k == 0) {
                count++;
            }
        }
        System.out.println(count);

    }
}

但是当我提交它时,它超时了。请帮助我尽可能优化它。

示例

输入:

7 3
1
51
966369
7
9
999996
11

输出:

4

他们说:

您应该能够处理 每个至少 2.5MB 输入数据 运行时第二个。

修改后的代码

谢谢大家...我修改了我的代码并且它起作用了...就在这里...

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int k = Integer.parseInt(input[1]);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (Integer.parseInt(br.readLine()) % k == 0) {
                count++;
            }
        }
        System.out.println(count);
    }

问候

shahensha

I am solving this question.

This is my code:

import java.io.IOException;
import java.util.Scanner;


public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] t = new int[n];
        int count = 0;
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            if (t[i] % k == 0) {
                count++;
            }
        }
        System.out.println(count);

    }
}

But when I submit it, it get's timed out. Please help me optimize this to as much as is possible.

Example

Input:

7 3
1
51
966369
7
9
999996
11

Output:

4

They say :

You are expected to be able to process
at least 2.5MB of input data per
second at runtime.

Modified CODE

Thank you all...I modified my code and it worked...here it is....

 public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int k = Integer.parseInt(input[1]);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (Integer.parseInt(br.readLine()) % k == 0) {
                count++;
            }
        }
        System.out.println(count);
    }

regards

shahensha

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅一夜美梦2024-10-24 00:22:03

这可能会稍微快一些,根据 limc 的解决方案,BufferedReader 应该会更快。

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int count = 0;
        while (true) {
            try {
                if (sc.nextInt() % k == 0) {
                    count++;
                }
            } catch (NoSuchElementException e) {
                break;
            }
        }
        System.out.println(count);

    }
}

This could be slightly faster, based on limc's solution, BufferedReader should be faster still though.

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int count = 0;
        while (true) {
            try {
                if (sc.nextInt() % k == 0) {
                    count++;
                }
            } catch (NoSuchElementException e) {
                break;
            }
        }
        System.out.println(count);

    }
}
别理我2024-10-24 00:22:03

这个怎么样?

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
    if (sc.nextInt() % k == 0) {
        count++;
    }
}
System.out.println(count);

How about this?

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
    if (sc.nextInt() % k == 0) {
        count++;
    }
}
System.out.println(count);
疧_╮線2024-10-24 00:22:03

您可以考虑读取大块输入,然后从那里获取数字。

其他变化是,您可以使用 Integer.parseInt() 而不是 Scanner.nextInt() 虽然我不知道每个的详细信息,但有些东西告诉我扫描仪版本执行更多计算以确定输入是否正确。另一种选择是自己转换数字(尽管 Integer.parseInt 应该足够快)

创建一个示例输入,并测量您的代码,在这里和那里进行一些更改,看看有什么区别。

测量,测量!

You may consider reading big chunks of input and then get the numbers from there.

Other change is, you may use Integer.parseInt() instead of Scanner.nextInt() although I don't know the details of each one, somethings tells me Scanner version performs a bit more computation to know if the input is correct. Another alternative is to convert the number yourself ( although Integer.parseInt should be fast enough )

Create a sample input, and measure your code, change a bit here and there and see what the difference is.

Measure, measure!

海未深2024-10-24 00:22:03

BufferedReader 应该比 Scanner 更快。不过,您需要自己解析所有内容,并且根据您的实现,情况可能会更糟。

BufferedReader is supposed to be faster than Scanner. You will need to parse everything yourself though and depending on your implementation it could be worse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文