猜数游戏 C语言程序设计

2025-03-13 02:52:40
推荐回答(3个)
回答1:

//猜1-100的随机数,有大小提示,并给出回答次数和所用时间的统计。
#include
#include
#include
void main()
{
int i,g,j=1;
long t;
srand((unsigned)time(NULL));
i = rand() % 100;
printf("请输入您猜的数字(1-100):");
scanf("%d",&g);
t=time(NULL);
while(g!=i)
{
if(g>i)printf("\n您猜的数字大了。请重输入:");
if(g scanf("%d",&g);
j++;
}
t=time(NULL)-t;
printf("\n恭喜您!回答正确。你猜了%d次,用时%d秒。\n",j,t);
}

回答2:

#include

#include

void
main()
{
int
Win
=
rand()
%
100000+
10000;
//随机赋值10000~99999
int
i
=
0;
int
n;
char
ch;
printf("please
input
n\n");
scanf("%d",&n);
while(1)
{
if(n
>
Win)
//猜的数字大了
{
printf("you
guess
number
big\n");
i++;
scanf("%d",&n);
}
if(n
<
Win)
//猜的数字小了
{
i++;
printf("you
guess
number
small\n");
scanf("%d",&n);
}
if(n
==
Win)
//
正确
{
i++;
printf("you
guees
is
right\n");
printf("你现在想退出吗?
输入a结束\n");
scanf("%c",&ch);
if(ch
==
'a')
break;
continue;
}
if(i
>
10)
//机会用完了
{
printf("你的机会已经用完,没机会再猜了\n");
break;
}
}
if(i
<
3)
printf("优秀\n");
//
对游戏者的评价
else
if(i
<
8)
printf("良好\n");
else
printf("
差\n");
}
这是我以前编过的程序,只是把范围改了你想要的

回答3:

#include
#include
#include
main()
{
int num,n;
time_t t;
srand((unsigned)time(&t));
num = rand();
while(1)
{
printf("please input a number: ");
scanf("%d",&n);
if(n>num)
{
printf("sry, your number is bigger than mine.\n");
}
else if(n {
printf("sry, your number is smaller than mine.\n");
}
else
{
printf("oh, you are so good ! your number is right!\n");
break;
}
}
}