二进制搜索 - 代码编译并运行后不显示输出
Binary Search - Output is not displaying after code compiles and runs
我现在刚刚在课堂上学习二进制搜索,下面的代码只是一个示例,因为我试图更好地理解它。所以话虽如此,这段代码正在编译但不显示任何输出,并且由于我对二进制搜索缺乏了解,我不知道为什么没有任何输出。有人可以指出写得很好的教程的方向吗?或者帮助指出代码出了什么问题。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
int thisArray[] = { 99,86,44,55,78,63,0,32,11 };
int num = 0;
int n = 0;
int first;
int last;
int middle;
first = 0;
last = n - 1;
middle = (first + last) / 2;
cout << "Enter the total number of elementsn";
cin >> n;
cout << "Entered " << n << "number.n";
for (int i = 0; i < n; i++) {
cin >> thisArray[i];
}
cout << "Enter a number to find.n";
cin >> num;
while (first <= last) {
if (thisArray[middle] < num) {
first = middle + 1;
}
else if (thisArray[middle] == num ) {
cout << num << " found at location " << middle + 1 << "n";
break;
}
else {
last = middle - 1;
}
middle = (first + last) / 2;
}
return 0;
}
编辑:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
//this is my binary search
int thisArray[10] = { 0,11,32,44,55,63,78,86,99 };
int i = 0; //index of the array
int n = 0; //variable of number that will be looked for
int first = 0;
int last = SIZE - 1;
int middle;
int pos = -1;
bool found = false;
int count = 0;
while (found) {
cout << "Enter a number to look for.n";
cin >> n;
while (first <= last) {
middle = first + last / 2;
if (thisArray[middle] == n) {
pos = middle;
cout << "item found at " << middle + 1 << "n";
exit(0);
}
else if (thisArray[middle] > n) {
last = middle - 1;
}
else {
first = middle + 1;
}//endif
}//end while
}//end big while
//if()
return 0;
}
知道了。感谢大家的帮助!
它没有输出任何东西,因为first == 0
和last == -1
.因此,first <= last
永远不会为真,循环体也永远不会被执行。
你知道吗? 如果数组没有正确排序以使用二叉搜索属性,您怎么能使用二叉搜索......
请停止像杰克·米格那样回答问题,我讨厌没有直接解决问题的答案,它有点重述问题......
简单
将last = n - 1;
更改为last = SIZE - 1;
或者在接受n
值后计算last = n - 1;
数组也需要排序!
想
通了。现在,虽然代码是一个例子,但我发现 for 循环对我来说无法正常工作,所以我摆脱了它。对于迭代,我实现了一个do-while循环。但是,代码现在可以工作了。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;
int SIZE = 10;
int main()
{
//this is my binary search
int thisArray[10] = { 0,11,32,44,55,63,78,86,99 };
int i = 0; //index of the array
int n = 0; //variable of number that will be looked for
int first = 0;
int last = SIZE - 1;
int middle;
int pos = -1;
bool found = false;
int count = 0;
do {
cout << "Enter a number to look for.n";
cin >> n;
while (first <= last) {
middle = first + last / 2;
if (thisArray[middle] == n) {
pos = middle;
cout << "item found at " << middle + 1;
exit(0);
}
else if (thisArray[middle] > n) {
last = middle - 1;
}
else {
first = middle + 1;
}//endif
}//end while
} while (found = true);
return 0;
}
相关文章:
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- C++ 构造函数未显示输出
- 显示C++输出而不返回值
- 为什么输出窗口上没有显示输出?
- 为什么我的代码块上没有显示输出?
- C++ 程序不显示输出
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- 如何从amd gpus的显示输出中获取图像数据
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 程序不会在必须时结束并显示输出
- 为什么我的前缀中缀代码不显示输出
- 函数显示输出和输入时出现问题
- 为什么我的程序多次显示输出
- 二进制搜索 - 代码编译并运行后不显示输出
- 字符串流未显示输出
- 为什么当我使用 cout 显示输出时,必须将按位运算符(例如 &,|,等)括在括号中?
- 这段代码可编译,但在visualstudio2015社区中运行(c++)时不显示输出
- Jupyter笔记本未显示C 输出(COUT)
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误
- Q未显示输出