我正在解决这个问题。
这是我的代码:
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
(沙亨莎)
int[] t
呢?你可以使用int t
代替。 - limc