猜数游戏c语言详解

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int i,g,j=1;
long t;
srand((unsigned)time(NULL));
i = rand() % 1000;
printf("I have a number between 1 and 1000.Can you guess my number?please type your first guess.:");
scanf("%d",&g);
t=time(NULL);
while(g!=i)
{
if(g>i)printf("\nToo low.Try again.:");
if(g<i)printf("\nToo high.Try again.:");
scanf("%d",&g);
j++;
}
t=time(NULL)-t;
printf("\nCongratulations!The correct answer.Guess %d times,When using %d secondds\n",j,t);
}
请在后面加上详解 谢谢

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
void main() 

int i, g, j = 1;
long t; 
srand((unsigned)time(NULL));  //初始化随机数种子
i = rand() % 1000; //随机产生数
printf("I have a number between 1 and 1000.Can you guess my number?please type your first guess.:");
scanf("%d", &g);
t = time(NULL);  //获取当前时间
while(g != i)
{
/*未猜中给出相应提示*/
 if(g > i)
     printf("\nToo low.Try again.:");  
 if(g < i)
     printf("\nToo high.Try again.:");
 scanf("%d", &g);
 j++;   //猜数次数
}
t = time(NULL)-t;   //得到猜数使用的时间
printf("\nCongratulations!The correct answer.Guess %d times,When using %d secondds\n", j, t);
}

 还需要解释的是随机数的问题,参加rand百科:http://baike.baidu.com/link?url=zLAaLl_41yftcOgI4oi1Lu7DFotxAIaq7D7uPVIlM5iMnY42_Jfyuavljahcm7lrQ-Ghvep4XjLGKm-XGYy5s_

注意rand不能实现真正意义的随机

温馨提示:答案为网友推荐,仅供参考
相似回答