用二分法实现猜数字:
当输入一个数字时,若大于所设定的,系统则报告太大,否则太小,然后会提示缩小范围来查找,直到猜到为止.
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int number;
srand((unsigned)time(NULL));
number=rand()%100;
int n,count=0;
cout<<"Enter the number you guess:";
cin>>n;
count++;
while(n!=number)
{
if(n>number)
{
cout<<"Too Big!"<<endl;
cout<<"Enter again:";
cin>>n;
count++;
}
else
{
cout<<"Too small!"<<endl;
cout<<"Enter again:";
cin>>n;
count++;
}
}
cout<<"The number you guess is "<<n<<" and the times you guess is "<<count<<endl;
}