gpt4 book ai didi

c++ - 实现二进制搜索猜谜游戏

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:57 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个程序,让计算机尝试猜测用户选择的数字 (1 - 100)。我已经把大部分程序写出来了,还做了一个随机数生成器。我现在唯一需要的是一种根据用户输入的 HIGH 或 LOW 将二分查找算法实现到我的程序中的方法。我已经阅读了一些关于二进制搜索算法的内容,但我不确定如何在我的程序中使用它。有人能指出我正确的方向吗?

第 34 和 42 行在应该有函数的地方有一个空白。这就是我想在我的程序中输入某种方程式来实现二进制搜索算法的地方。

下面是我目前的代码:

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
#include <windows.h>
using namespace std;

int main()
{
int highLow;
int yesNo;
int ranNum;
srand( time(0));


ranNum = rand() % 100 + 1;

cout << "Please think of a number between 1-100. I'm going to try to guess it.\n";
_sleep(3000);

cout << "I'm going to make a guess. Is it "<< ranNum <<" (1 for yes and 2 for no)?\n";
cin >> yesNo;


while (yesNo == 2)
{
cout << "Please tell me if my guess was higher or lower than your number\n";
cout << "by inputting 3 for HIGHER and 4 for LOWER.\n";
cin >> highLow;

if (highLow == 3)
{
cout << "Okay, so my guess was higher than your number. Let me try again.\n";
_sleep (1500);
cout << "Was your number " << <<"? If yes, input 1. If not, input 2.\n";// I would like to find a way to implement the
//binary search algorithm in this line of code.
cin >> yesNo;
}
if (highLow == 4)
{
cout << "Okay, so my guess was lower than your number. Let me try again.\n";
_sleep (1500);
cout << "Was your number " << <<"? If yes, input 1. If not, input 2.\n";// I would like to find a way to implement the
//binary search algorithm in this line of code.
cin >> yesNo;
}
}
if (yesNo == 1)
{
cout << "My guess was correct!\n";
}
}

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