用C、C++ 编写猜数字小游戏,比较急,希望能在今天之内解决,下面补充有详细要求。谢谢了!

2024年11月15日 08:27
有3个网友回答
网友(1):

该程序是可以产生真正的随机数,我设置该程序可以让人猜10次。啧啧!平均猜数,LZ修改一下就可以了
#include "stdlib.h"
#include "stdio.h"
#include "time.h"
void main( void )
{
int i,num,n,m;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers:0~9 */

for( i = 0; i < 10;i++ )
{
num=rand() % 100;
if(num==0)
num=100;
printf("请输入你猜的数\n");
m=0;
while(scanf("%d",&n))
{
m++;
if(n==num)
{
printf("恭喜您,猜对啦!");break;
}
if(n>num)
printf("太大啦!\n");
if(nprintf("大小啦!\n");

}
printf("猜的次数为%d\n",m);
}

//printf( " %6d\n", rand() % 100 ); 此处是产生的随机数的值。

}

网友(2):

#include
#include
#include
#define M 100
int main()
{
int a,b,i,j,k,avg,sum;
int s[M]={0};

srand(time(NULL));
a = rand()%99;
printf("%d\n",a);
i=0;
j = 1;
sum =0;
while(1)
{
printf("Please input the num\n");
scanf("%d",&b);
if(a == b)
{
printf("you are right\n");
i++;
k = i;
printf("you need %d times\n",i);
i=0;
sum = sum + k;
avg = sum/j;
printf("the avg times is %d\n",avg);
j++;
printf("j=%d\n",j);
a = rand()%99;
printf("%d\n",a);
continue;
}
else if(a>b)
{
printf("the num is small\n");
i++;
continue;
}
else if(a {
printf("the num is big\n");
i++;
continue;
}
}
return 0;
}

希望对你有帮助,

网友(3):

我用matlab变过。。很简单的