含泪的微笑
传说中的水手
 
UID 29980
精华
0
积分 4
帖子 4
阅读权限 1
注册 2007-4-19
状态 离线
|
我知道了正确答案
正确的是:
#include<iostream.h>
#include<stdlib.h>
#include<dos.h>
int main()
{
int max,min,i,k,n;
struct time t;
gettime(&t);
srand(t.ti_hund);
for(k=1;k<101;k++)
{
n=0,max=100,min=1; //每猜一个数时都应该是在1到100之间的
for(i=0;i<1000;i++)
{
while(min<max)
{
int temp=rand()%(max-min+1)+min;
if(temp>k) max=temp-1;
if(temp<k) min=temp+1;
if(temp==k) {n++;break;}
n++;
}
cout<<n/1000.0;
}
}
getchar();
return 0;
}
每个数字猜1000次,但是每次必须猜中,最后输出的是猜中的平均次数
例如:猜测 50时,第一次用了5次猜中,第二次用了10次……直到第1000次用了8次
最后输出的是(5+10+…+8)/1000.0
所以while 是必须的
[ 本帖最后由 含泪的微笑 于 2007-5-7 17:07 编辑 ]
|
|