Answer the question
In order to leave comments, you need to log in
An exercise from Stroustrup's book. Guess the number program. Can you write better?
Hello.
I did an exercise for Bjarne Stroustrup's book "Programming. Principles and practice
of using C++".
The text of the exercise is:
Write a program to guess the number. The user must think
of a number from 1 to 100, and the program must ask questions to find out what
number he has thought of (for example, "Thought number less than 50"). Your program
should be able to identify the number after no more than seven tries. Hint
: use the < and <= operators and if-else constructs.
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
double curr =50;
double shift =50;
char answer = ' ';
bool equal_check = false;
for(int i=0;i<8;i++){
cout << "Число равно" << static_cast<int>(curr + 0.5) <<"?(y,n)";
cin >> answer;
if (answer=='y') { equal_check = true; break; }
cout << "Число меньше " << static_cast<int>(curr + 0.5) << "?(y,n)";
cin >> answer;
cout << "shift" << shift << endl;
cout << "curr" << curr << endl;
if (answer=='y') { shift=shift/2; curr -= shift; }
else if(answer=='n') { shift=shift/2; curr += shift; }
}
if (!equal_check)
{
cout << "wrong input" << endl;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
double curr =50, shift =50;
char answer = ' ';
cout << "Загадайте число от 1 до 100." << endl;
system("pause");
for(int i=0;i<8;i++){
cout << "Число меньше " << static_cast<int>(curr + 0.5) << "?(y,n)";
cin >> answer;
if (answer=='y'){
shift=shift/2;
curr -= shift;
}
else if(answer=='n'){
shift=shift/2;
curr += shift;
}
if ( (curr <= 1.0) || (curr >= 99.5) ){
cout <<"Ваше число " << static_cast<int>(curr + 0.5) << endl;
break;
};
if ( shift <= 0.5){
cout <<"Ваше число " << static_cast<int>(curr) << endl;
break;
}
}
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int min = 1, max = 100;
cout << "Загадайте число от " << min << " до " << (max - 1) << ".\n"; //микрофикс
int guess = (max + min) / 2, count = 7;//микрофикс (C++98)
for (int i = 0; i < count; ++i)
{
cout << "Загаданное число меньше " << guess << "? (y/n)\n";
char ans;
cin >> ans;
if (ans == 'y')
{
max = guess;
}
else
{
min = guess;
}
guess = (min + max) / 2;
if (max - min < 2)
{
cout << "Вы загадали число \"" << guess << "\".\n";
system("PAUSE");
return 0;
}
}
cout << "Жулик!\n";
system("PAUSE");
return 0;
}
Answer the question
In order to leave comments, you need to log in
Understood correctly, this is an ordinary binary search. It is usually implemented recursively, you can look at the implementation, for example, on Wikipedia .
You can also write better - with line breaks and spaces it will look much nicer.
Ps You have 8 loop passes.
upd. If we assume that every question (no matter what) is an attempt, then we can limit ourselves to only seven questions. For example, like this:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
int min = 1, max = 100;
cout << "Загадайте число от " << min << " до " << max << ".\n";
int guess = (min + max) / 2, count = (int) ceil(std::log2(max - min));
for (int i = 0; i < count; ++i)
{
cout << "Загаданное число меньше или равно " << guess << "? (y/n)\n"; // fixed
char ans;
cin >> ans;
if (ans == 'y')
{
max = guess;
}
else
{
min = guess + 1; // fixed
}
guess = (min + max) / 2;
if (max == min) // fixed
{
cout << "Вы загадали число \"" << guess << "\".\n";
system("PAUSE");
return 0;
}
}
cout << "Жулик!\n";
system("PAUSE");
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question