gpt4 book ai didi

java - 如何减少这个程序的执行时间

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:11:43 25 4
gpt4 key购买 nike

<分区>

int n, k;
int count = 0, diff;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input;
input = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
int[] a = new int[n];
k = Integer.parseInt(input[1]);
input = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(input[i]);
for (int j = 0; j < i; j++) {
diff = a[j] - a[i];
if (diff == k || -diff == k) {
count++;
}
}
}
System.out.print(count);

这是一个示例程序,我在其中打印特定的差异计数,其中 n 范围 <=100000现在的问题是减少这个程序的执行。我怎样才能更好地减少运行时间。

提前感谢您的建议

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com