C++ 在C+上尝试数字猜测游戏+;

C++ 在C+上尝试数字猜测游戏+;,c++,binary-search,C++,Binary Search,我正在尝试一个我发现的数字猜测游戏,似乎无论我选择什么,它总是说我选择的数字小于或大于。我想用二进制搜索实现它,但不知道如何实现。我怎样才能做到 代码: #include <cstdlib> #include <time.h> #include <iostream> using namespace std; int main() { srand(time(0)); int number; number = rand()

我正在尝试一个我发现的数字猜测游戏,似乎无论我选择什么,它总是说我选择的数字小于或大于。我想用二进制搜索实现它,但不知道如何实现。我怎样才能做到

代码:

#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
srand(时间(0));
整数;
数字=兰德()%100+1;
智力猜测;
做{
猜不透;
如果(猜测<数字)

这应该可以做这件事

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;
int guessNum(int lb,int ub,int number){
    int lowerBound=lb,upperBound=ub;
    int guess = (lowerBound+upperBound)/2;
    if (number>guess){
        lowerBound = guess;
        guessNum(lowerBound,upperBound,number);
    }
    else if(number < guess){
        upperBound=guess;
        guessNum(lowerBound,upperBound,number);
    }

    else
        return guess;
}

int main() {
    srand(time(NULL));
    int number;
    number = rand() % 100 + 1;
    int guess;

          std::cout<<number << " = " <<guessNum(1,100,number);


    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int guessNum(int-lb,int-ub,int-number){
int lowerBound=lb,上限=ub;
int guess=(下限+上限)/2;
如果(数字>猜测){
lowerBound=猜测;
guessNum(下限、上限、数字);
}
else if(数字<猜测){
上限=猜测;
guessNum(下限、上限、数字);
}
其他的
返回猜测;
}
int main(){
srand(时间(空));
整数;
数字=兰德()%100+1;
智力猜测;
可以理解游戏吗
软件随机选择一个介于0-100之间的数字,你需要找到它?对吗?软件给了你一些线索,但你从未找到这个数字。那么,解决方案是什么

欺骗游戏 当事情出错时,我更愿意说清楚。所以,向软件询问号码,你就会知道是哪个号码。每当我需要知道幕后发生了什么时,我都会这样做

#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;
            // with this line ↓ you could see what is happen
            cout << "Your number is " << guess << " and the secret number is " << number << endl;
            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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
srand(时间(0));
整数;
数字=兰德()%100+1;
智力猜测;
做{
猜不透;
//用这条线↓ 你可以看到发生了什么

你是想编码自动猜数字,还是想让用户猜数字?我想让用户猜,例如,秘密号码是58,用户一直试图键入一些数字,如果他得到了,他会,如果没有,程序会不断询问。我使用了rand()为了使游戏更有趣,因为它总是生成一个随机数。如果用户是进行搜索的用户,您到底想实现什么样的二进制搜索?@Uxellodunon是的。这是一个示例,说明了一个人如何进行二进制搜索以查找值。这不完全是真正的二进制搜索,因为用户不必猜测新的值中间点,但他们应该猜测一个新的数字,该数字更接近要猜测的数字,因为他们知道数字的方向。@Uxellodunon尝试制作一个简单的hello world程序。一旦你成功,只需将这个问题中的代码复制并通过hello world程序中的代码重新编译。我的理解是用户正在尝试猜测计算机的数字。以您的示例为例,计算机正在猜测一个随机数。@ThomasMatthews在对答案进行向下表决之前,请阅读OP“我想使用二进制搜索实现它,但不知道如何实现。我如何才能实现它?”很可能实施二进制搜索意味着计算机通过实施二进制搜索来查找随机数,但同样,我可能会误读OP评论中的这一行:“我希望用户猜测它,例如,秘密号码是58,用户一直试图键入一些号码,如果他得到了,他会这样做,如果没有,程序会继续询问。”