一本c++书中的练习题
所用代码:geofhagopian.net/CS007A/CS7A-F15/GH_PPP4-e4.cpp
测试时发现一个bug,比如100就猜不出来,小修了一下
#include "../std_lib_facilities.h"
int main()
{
cout << "\nThink of a number between 1 and 100";
int left = 1, right = 100;
bool found = 0, yes = 0;
while (!found) {
cout << "\nIs your number " << (left + right) / 2 << "? ";
cin >> yes;
if (yes) {
found = 1;
break;
}
cout << "\nIs your number greater than " << (left + right) / 2 << "? ";
cin >> yes;
if (yes) left = (left + right) / 2 + 1;
else right = (left + right) / 2;
}
if (found) cout << "\nHooray, you found the number is " << (left + right) / 2;
else cout << "\nFailure!";
}