gpt4 book ai didi

java - 顺序搜索 10000 个随机数文件中的 1000 个键

转载 作者:行者123 更新时间:2023-12-02 07:41:46 25 4
gpt4 key购买 nike

我编写了一些代码,它将在一个包含 10000 个随机数的文件中顺序搜索 1000 个键,并打印出找到的键。

这是下面的代码。当我运行它但我没有得到任何东西。我的错误在哪里?

public class SeqSample2 {

public static void main(String[] args) {
double[] a;
double[] b;
a = new double[10000];
b = new double[1000];
for (int i = 0; i < 1000; i++) {
a[i] = (int) ((Math.random() * 10000));
}
for (int j = 0; j < 1000; j++) {
b[j] = (int) ((Math.random() * 1000));
}

int lim = a.length - b.length;
int i = 0;
int j = 0;
boolean found = false;
for (i = 0; i < lim; i++) {

if (b[0] == a[i]) {
found = true;

for (j = 0; j < b.length; j++) {
if (b[j] != a[i + j]) {
found = false;
break;
}
}
if (found) {
System.out.println(b[i]);
} else {
System.out.println("Not found");
}
}
}
}
}

最佳答案

你为什么感到惊讶?

  • b[0]是 0 到 1,000 之间的随机数。
  • a[i] i < 1,000 是 0 到 10,000 之间的随机数0 表示 i >= 1,000 .

如果你运行你的程序几次,你最终会得到一个输出,但大多数时候,if (b[0] == a[i])将为 false,程序将退出而不打印任何内容。

关于java - 顺序搜索 10000 个随机数文件中的 1000 个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11483753/

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