- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么此二分搜索代码在 Eclipse IDE 上给出错误的输出,但在提交到 Coursera 时却被接受?这是一个示例输入,它显示了错误的输出。
示例输入:
5 3 2 4 1 5
3 1 2 7
输出:
-1 -1 -1
显然,存在的元素“1”是输入数组。但其输出是 -1 而不是 3。
import java.io.*;
import java.util.*;
public class BinarySearch {
static int binarySearch(int[] a,int l,int r,int x) {
//write your code here
if(l<=r){
int mid =l + (r - l)/2;
if(x==a[mid])
return mid;
else if(x<a[mid]){
return binarySearch(a,l,mid-1,x);
}
else
return binarySearch(a,mid+1,r,x);
}
return -1;
}
public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int m = scanner.nextInt();
int[] b = new int[m];
for (int i = 0; i < m; i++) {
b[i] = scanner.nextInt();
}
for (int i = 0; i < m; i++) {
//replace with the call to binarySearch when implemented
System.out.print(binarySearch(a,0,n-1,b[i]) + " ");
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
最佳答案
实际上,问题出在您的输入数据上,而不是代码本身。如果您搜索有关二分搜索的信息,您可以找到:“二分搜索是一种搜索算法,用于在排序数组中查找目标值的位置”。您的输入未排序。
在运行搜索之前,您必须对数组进行排序,这将是一个坏主意 - 使用其他算法进行搜索比排序花费的时间更少。
如果您尝试输入排序数据,例如:
5 1 2 3 4 5
3 1 2 7
结果将是 0 1 -1
- 正如预期的那样。
关于java - 为什么此二进制搜索代码在 Eclipse IDE 上给出错误的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61727328/
我正在寻找一个编辑器/IDE,它可以为不是专门为它构建的语言提供在编码时很好的功能(例如:能够点击到函数定义)。通过这些,我想到了为非常特定的目的而设计的语言,并且通常只被一个小社区使用。换句话说,那
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
有简单的解释性编程语言,实际上还有控制台 interpreter.exe。 需要按 F5 对语法进行着色、自动完成和执行。 (如果有可能进行“调试”——那就太棒了!) 我从来没有做过这样的事情。 有许
我只是想知道是否有任何可以在我的黑莓上运行的 IDE。我的旧 Palm 有 C 和 BASIC 翻译。 C 板上使用 Palm 的内置文本编辑器,但 BASIC 解释器内置了自己的简单编辑器。 黑莓或
我正在制作一个文件编辑器,并希望为我的用户提供一个不错的 IDE,可以在其中在浏览器上编辑他们的 html/css 文件。是否有任何编辑器与 TinyMCE 类似,但它不是一个所见即所得的编辑器,它更
是否有一个带有集成调试器的D IDE? 最佳答案 Descent可以使用调试器。不完全是您的要求,但是... 关于ide - 是否有一个带有集成调试器的D IDE?,我们在Stack Overflow
每个程序员都知道工具很重要,对于开发人员来说,没有比用于编码的 IDE 更重要的工具了。在过去的几年里,IDE-s 成为标准,在这个领域看到创新并不常见。您可以推荐哪些 IDE 具有创新性,它们引入了
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我目前正在学习Ironpython和热爱软件,但我希望从使用notepad ++和cmd.exe继续前进,并尝试使用果汁多一点的东西。 我最近了解到Iron python studio不支持Iron
我主要从事 Java 和 C/C++ 开发,但我开始做更多的 Web 开发(PHP、Rails)和 Eiffel(学习一门新语言总是好的)。 目前,我使用 Eclipse for Java、C/C++
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
是否有 280Atlas (280atlas.com)(免费/商业)的替代品? 其中哪些是成熟的? 最佳答案 您可以使用 nib2cib ,这几乎是相同的想法,但它使用界面构建器(xcode的一部分)
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
IDE 的目标是提高生产力。他们在这方面做得很好。重构、导航、内联文档、自动完成有助于极大地提高生产力。 但是:每个工具都是武器 .相同的 IDE 有助于生成块代码。一些 IDE 功能会导致产生不良代
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我正在寻找一些功能强大的C / C ++编程环境。实际上,我唯一需要的就是强大的源导航+创建工具。免费或商业都无所谓。我更喜欢一些linux工具,但是它不一定是必需的linux应用。 我需要的是一种具
我刚开始学习 D。有人知 Prop 有自动格式化功能的 D IDE 吗? Eclipse 的 DDT 似乎除了语法高亮之外没有任何其他功能。 最佳答案 我相信,目前 MonoDevelop + Mon
我有兴趣为一个副项目构建一个新风格的 IDE。主要是为了取消类固醇IDE上的普通记事本。我正在为已经尝试过的或者你已经看到(或没有看到)看起来很酷并且在 IDE 中有用的东西寻找一些灵感。我可以解决的
我需要维护一些 VB6 应用程序,并且在涉及枚举名称时遇到了一个奇怪的问题。 VB6 中的 Intellisense 应该工作的方式是,如果我的变量名称被定义为,例如,Dim Abraxis as S
正如标题所说,我看到很多编辑将宏录制作为一项功能吹捧,但自己却找不到利用这些功能的方法。那你能用它做什么呢?您可以记录鼠标移动和/或击键的类型?对外面的人真的那么有帮助吗?具体来说,我处理的 Ecli
我是一名优秀的程序员,十分优秀!