c++ 猜数字游戏猜1-100间的一个整数
一本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!";}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
