一个猜数字小游戏

na_na_lu 2007-05-03 04:36:41

#include<iostream.h>
#include<stdlib.h>
#include<dos.h>
int main()
{
int max=100,min=1,i,k,n;
struct time t;
gettime(&t);
srand(t.ti_hund);
for(k=1;k<101;k++)
{
n=0;
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;
}

我的执行结果是死循环,请问哪句有问题?
...全文
264 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
na_na_lu 2007-05-03
  • 打赏
  • 举报
回复

#include<iostream.h>
#include<stdlib.h>
#include<dos.h>
int main()
{
int max=100,min=1,i,k,n; //i为次数,n为猜中一个数字需要的次数
struct time t; //以系统时间的秒作为随机数的种子
gettime(&t);
srand(t.ti_hund);
for(k=1;k<101;k++) //1到100之间的 每个数猜中的频率
{
n=0;
for(i=0;i<1000;i++) //每个数猜1000次,最后输入猜中每个数的频率
{
while(min<max)
{
int temp=rand()%(max-min+1)+min; //temp为min到max之间的随机数
if(temp>k) max=temp-1;
if(temp<k) min=temp+1;
if(temp==k)
{n++;break;} //如果猜中,则跳出循环
n++;
}
cout<<n/1000.0; //输出每个数猜1000次 猜中的平均的次数
}
}
getchar();
return 0;
}
v风雪山神庙v 2007-05-03
  • 打赏
  • 举报
回复
//猜数字小游戏,070206-23:21

#include <iostream.h>
#include <time.h>
#include <stdlib.h>


int LocateElem(int *p,int elem)
{
//在p数组中搜寻elem元素,如有则返回下标,否则返回0
for(int i=1;i<5;i++)
if(p[i]==elem) return i;
return 0;
}

int * CreateArray()
{
//创建随机数组,即为要猜的4个数,这4个数都是个位数
int *p=new int [5];
for(int i=0;i<5;i++)
p[i]=-1;
srand(time(0));
i=1;
while(i<5)
{
int t=rand()%10;
if(!LocateElem(p,t))
p[i++]=t;
}
return p;
}


void Output(int *p)
{
//输出答案
for(int i=1;i<5;i++)
cout<<p[i]<<' ';
cout<<endl;
}

void main()
{
int * arr;
int userdata[5];
int cur;
int cnt1=0,cnt2=0;
arr=CreateArray();
cout<<"说明:(AxBy) x表示你输入的4个数中有x个数和问题数相等且位置一样\n"
<<"y表示你输入的4个数中有y个数和问题数相等但位置不同"<<endl;
cout<<"请输入四个数"<<endl;
int n=0;
while(n<8)
{
//n<8表明只允许用户输入8次,超过8次则自动输出答案
cnt1=cnt2=0; //cnt1表示x,cnt2表示y
for(int i=1;i<5;i++)
cin>>userdata[i];
for(i=1;i<5;i++)
if(cur=LocateElem(arr,userdata[i]))
if(cur==i) ++cnt1;
else ++cnt2;
if(cnt1==4) {cout<<"恭喜你,答对了"<<endl;break;}
else
{
cout<<'A'<<cnt1<<'B'<<cnt2<<endl;
++n;
}
}
cout<<"答案是"<<endl;
Output(arr);
}
na_na_lu 2007-05-03
  • 打赏
  • 举报
回复
你用的什么环境?
我用C++ Builder
可以编译通过的
helloworldbaby 2007-05-03
  • 打赏
  • 举报
回复
struct time t;
gettime(&t);
srand(t.ti_hund);
getchar();
这四个语句都有问题, 本人水平有限还是等高手来解决吧!
关注!!
「已注销」 2007-05-03
  • 打赏
  • 举报
回复
struct time t;是什么东西啊?
获得时间不是这么写的吧?我编译都通不过,有4个错误。

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧