#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不能实现真正意义的随机