在 Java 中获取输入最有效的方式是什么?
我正在解决这个问题。
这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能会稍微快一些,根据 limc 的解决方案,BufferedReader 应该会更快。
This could be slightly faster, based on limc's solution,
BufferedReader
should be faster still though.这个怎么样?
How about this?
您可以考虑读取大块输入,然后从那里获取数字。
其他变化是,您可以使用
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 ofScanner.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!
BufferedReader
应该比Scanner
更快。不过,您需要自己解析所有内容,并且根据您的实现,情况可能会更糟。BufferedReader
is supposed to be faster thanScanner
. You will need to parse everything yourself though and depending on your implementation it could be worse.