C++计算机猜测用户猜测数在7猜测之内

C++计算机猜测用户猜测数在7猜测之内,c++,loops,binary-search,C++,Loops,Binary Search,下面的提示是关于我对计算机科学课程期末考试的介绍 根据以下说明编写程序: 用户选择数字并告诉计算机其猜测是过高还是过低,直到计算机猜对为止。 如果操作正确,程序应该能够在7次猜测中确定数字,假设人类没有作弊。 提示:二进制搜索–在可能的剩余数字之间的中间位置进行尝试。 为您的程序提供测试值和预期结果 我怎样才能让它在7次尝试中猜出来 #include <time.h> #include <iostream> using namespace std; int main()

下面的提示是关于我对计算机科学课程期末考试的介绍

根据以下说明编写程序: 用户选择数字并告诉计算机其猜测是过高还是过低,直到计算机猜对为止。 如果操作正确,程序应该能够在7次猜测中确定数字,假设人类没有作弊。 提示:二进制搜索–在可能的剩余数字之间的中间位置进行尝试。 为您的程序提供测试值和预期结果

我怎样才能让它在7次尝试中猜出来

#include <time.h>
#include <iostream>
using namespace std;

int main()
{ 
 int min,max;
 cout<<"To begin guessing game, enter maximum and minimum game parameters (positive integers).\n";
 cout<<"Enter minimum value : ";
 cin>>min;
 cout<<"Enter maximum value : ";
 cin>>max;
 cout<< "Choose a number between "<<min<< " and "<<max<<" : ";
 int userNum;// create variable A
 cin>>userNum;//User inputs number 
 int compGuess;
 srand(time(NULL));
 compGuess = rand() % max + min;//computer produces random number between 1 and 100 and stores in variable b

    
        while (userNum != compGuess)//compare variable to a to b
        {
            cout<<"Computer guesses "<<compGuess;
            cout<<endl;
            cout<<"Is your number higher? (enter y for yes or n for no) : ";
            char c;//char size variable created called c
            cin>>c;//User input (y or n)  overwrites c
            bool d;//bool size variable created called d
                if (c=='y')
                    {
                        d=true;
                        min=compGuess;
                    }
                else  
                    {
                        d=false;
                        max=compGuess;
                    }
                    
                if (d)
                    {
                        compGuess=(compGuess+max)/2;
                    }
                else
                    {
                        compGuess=(min+compGuess)/2;  
                    }
        }   
        cout<< "Your number is "<<compGuess;   
        
}        
    
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 





        
        

你应该试着从计算机的角度思考,因为计算机不知道答案

当你从玩家那里得到答案时,你会从电脑中获得信息。该信息是未知数字所在的范围。即最小值和最大值。将其初始化为0,100。每次你从玩家那里得到答案时更新这个。重复此操作,直到最小值=最大值


当心。

那么你的问题到底是什么呢?提供的二进制搜索描述足以让你在考试中找到答案。简单地测试记忆的考试并不是那么好。看起来你也没有学会值(如“y”)和变量标识符(如y)之间的区别。或者对变量使用有意义的名称。你不能问玩家他们在想什么数字。游戏的关键是程序不知道。