<分区>
我正在尝试我发现的一个数字猜谜游戏,似乎无论我选择什么,它总是说我选择的数字小于或大于。我想使用二进制搜索来实现它,但不知道该怎么做。我怎样才能实现它?
代码:
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;
int main() {
srand(time(0));
int number;
number = rand() % 100 + 1;
int guess;
do {
cout << "Enter a number of your choice b/w 1-100: ";
cin >> guess;
if (guess < number)
cout << "Sorry, try again, it's smaller than the secret number!" << endl;
else if (guess > number)
cout << "Sorry, try again, it's bigger than the secret number!" << endl;
else
cout << "The number is correct! Congratulations!" << endl;
} while (guess != number);
system("PAUSE");
return 0;
}