把刚才发的改进了下余裤,可以猜一个的数的范围为0≤ N ≤4294967295,你不用输入,只需要想一个数字:
#include
#include
#include
#include
using namespace std;
int main()
{
srand((unsigned)time(0));
cout << "你想一个数字然后我来猜, 告诉我我猜的是大了(D)还是小了(X).\n";
unsigned low = 0, high = 1;
bool guessed = false;
bool flagh = false, flagl = false;
int guess = 0;
unsigned count = 1;
while(!guessed)
{
if(count == 1)
guess = rand();
else if(flagl && !flagh)
guess = rand() % (4294967295 - low);
else if(flagh && !flagl)
guess = rand() % high;
else if(flagl && flagh)
guess = low + rand() %(high - low + 1);
cout << "\n我猜的是第 " << count << " 个数字是: " << guess << '.' <<竖正简 endl;
cout << "大了(D), 小了(X), 或猜到了(G)? ";
string response;
cin >> response;
if(low == high)
{
cout << "你作弊, 不和你玩了清圆~!" << endl;
return EXIT_FAILURE;
}
switch(toupper(response[0]))
{
case 'D':
high = guess;
flagh = true;
break;
case 'X':
low = guess;
flagl = true;
break;
case 'G':
guessed = true;
break;
default:
cout << "错误的选择!\n";
continue;
}
++count;
}
cout << "\n我猜到了! 你想的数字是: " << guess << '\n';
}
#defin N 100
void mian()
{
int a=0;
while(1)
{
cout << "请输入数据:";
cin >> a;
if(a>N)
{
cout << endl << "太大了!" << endl;
}
else if(a
{
cout << endl << "太大了!" <裤码< endl;
}
else if(a==N)
{
cout <如野< endl << "完全正确!你太有渣纯喊才了。" << endl;
break;
}
}
}
}
}
二分法嘛。