用二分法实现猜数字:
当输入一个数字时,若大于所设定的,系统则报告太大,否则太小,然后会提示缩小范围来查找,直到猜到为止.
用二分法实现猜数字:
当输入一个数字时,若大于所设定的,系统则报告太大,否则太小,然后会提示缩小范围来查找,直到猜到为止.
2007-07-06 23:01
2007-07-06 23:05
2007-07-06 23:23
2007-07-06 23:27
2007-07-06 23:30
2007-07-06 23:45
诸位有的人不明白我的意思我再说详细点:要求用二分法猜数字,比如原始设定数字为17,当你输入89时,系统则会提示太大,再输入15时,则提示比较接近,这样精度会更高,不会乱猜下去的,那位能帮我把算法写出来,兄弟我万分感激,兄弟我还在初学C语言,希望能得到大家的帮助!!!!

2007-07-08 20:48
2007-07-08 21:09
2007-07-09 14:39
#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;
}

2007-07-09 20:53