write a numbers guessing game | Sololearn: Learn to code for FREE!

+3

write a numbers guessing game

Write a program to play a numbers guessing game. The user thinks of a number between 1 and 100 and your program asks questions to figure out what the number is (e.g., "Is the number you are thinking of less than 50?"). Your program should be able to identify the number after asking no more than seven questions. Help me!

12/22/2016 12:30:17 PM

惠天宇

2 Answers

New Answer

+1

Two of my friends have thought of an answer to this question. Code: #include <iostream> using namespace std; //a numbers guessing game int main() { string answer = ""; int guess_max = 100; int guess_min = 1; while (guess_min != guess_max) { cout << "Is the number you are thinking of less than " << (guess_max + guess_min + 1) / 2 << "?\n"; cin >> answer; if (answer == "y" || answer == "Y" || answer == "yes" || answer == "Yes") guess_max = (guess_max + guess_min) / 2; else if (answer == "n" || answer == "N" || answer == "no" || answer == "No") guess_min = (guess_max + guess_min + 1) / 2; else { cout << "Bad input!\n"; } } cout << "The number you are thinking of is " << guess_min << endl; }

0

wow!! try binary search for it. if you don't known about binary search, google it. or search data structure book.