【问题标题】:C++ Making a computer guess a numberC++ 让计算机猜一个数字
【发布时间】:2016-12-11 02:47:36
【问题描述】:

试图弄清楚如何让计算机猜测一个 9 位二进制数 7 次,以及何时输出输入的数字正确。

另外,如果有人知道如何计算二进制数中 0 和 1 的数量,我将不胜感激,我尝试在 std::count() 上阅读,但由于我对 c++ 很陌生,所以不明白。

干杯。

 void breakyourcode()
    {
        cout << "You have picked break your code" << endl;
        string guess;
        int tries = 7;
        srand(time(NULL));
        int index = rand() % 512;
        string cGuess = index2code(index);
        cout << cGuess << endl;

        cout << "Enter a number: ";
        cin >> guess;

        do {
            cout << "\nComputer's guess: " << endl;
            cout << cGuess;
            ++tries;

            if (guess == cGuess) {
                cout << "Yeeha! I won!" << endl;
                system("PAUSE");
                break;
            }
            cGuess = rand() % 512;

        } while (guess != cGuess);

        return;
    }

【问题讨论】:

    标签: c++ count binary


    【解决方案1】:

    在我对您的问题的解释中,我假设您想要一个无符号数字——尽管我想这也适用于有符号整数。

    我采取的另一个假设是您希望用户输入数字作为 1 和 0,将其放入字符串中,然后通过函数 convertBinaryNumberStringToInt 转换为 unsigned 整数。

    这里有一些代码建议你如何做到这一点,包括函数howManyOnes,专门用于计算你的数字的二进制形式中有多少。

    #include <iostream>
    #include <string>
    
    // For CHAR_BIT
    #include <climits>
    
    // For std::reverse, but you could
    // write your own string reverse function
    #include <algorithm>
    
    unsigned convertBinaryNumberStringToInt(const std::string& stringNumber)
    {
        unsigned outputNumber = 0;
    
        // Using a reversed string allows us to insert ones in the
        // number's binary form from right to left while still using
        // a range-based for loop with not much tinkering
        std::string reversedStrNum = stringNumber;
        std::reverse(reversedStrNum.begin(), reversedStrNum.end());
    
    
        unsigned i = 1;
    
        // i starts as 00000000000000000000000000000001 (binary, assuming 32 bits integer)
        // On each iteration of the for loop, the 1 is shifted left and,
        // if the current character is '1', the value 1 is copied into
        // its corresponding current position in outputNumber
        for(char c : reversedStrNum)
        {
            if(c == '1')
            {
                // The bitwise-or operation copies the 1 in i to
                // its corresponding position in outputNumber
                outputNumber = outputNumber | i;
    
            }
            // Shift i's 1 to the left by one position
            i = i << 1;
            // You could also do i *= 2, but we're specifically shifting
            // bits, so the bitshift seems more "semantic"
        }
    
        return outputNumber;
    }
    
    bool isStringCorrect(std::string& userString, size_t maxSize)
    {
        if(userString.size() > 9)
            return false;
    
        for(char c : userString)
            if(c != '1' && c != '0')
                return false;
    
        return true;
    }
    
    unsigned computerGuess(unsigned highBound)
    {
        return rand() % highBound;
    }
    
    unsigned howManyOnes(unsigned number)
    {
        unsigned count = 0;
        unsigned shifter = 1;
    
        // For each bit: sizeof(unsigned) is the size in bytes
        // of an unsigned, CHAR_BIT is the bit length of a char or byte
        // So we loop through each bit position.
        for(unsigned i = 0; i < CHAR_BIT * sizeof(unsigned); i++)
        {
            // What we do is we test our number against the shifter
            // for each bit position. If a bitwise-and between both
            // is non-zero, it means number has 1 in that position,
            // so we increase the "one" count.
            if((number & shifter) != 0)
                ++count;
    
            // Either way, we left-shift the one in shifter afterwards
            shifter = shifter << 1;
        }
    
        return count;
    }
    
    int main()
    {
        std::string userChoiceNumberString;
        std::cout << "Enter a number: ";
        std::cin >> userChoiceNumberString;
    
        if(!isStringCorrect(userChoiceNumberString, 9))
        {
            std::cout << "Entered string is incorrect" << std::endl;
            return -1;
        }
    
        unsigned userChoiceNumber = convertBinaryNumberStringToInt(userChoiceNumberString);
    
        std::cout << "Your number has " << howManyOnes(userChoiceNumber) << " ones in binary." << std::endl;
    
        int attempts = 7;
        while(attempts > 0)
        {
            if(computerGuess(512) == userChoiceNumber)
            {
                std::cout << "Yeeha! I won!" << std::endl;
                attempts = 0;
            }
            else
            {
                std::cout << "Hmmm... guessed wrong..." << std::endl;
                --attempts;
            }
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      相关资源
      最近更新 更多