1.机猜(完全无脑,图个乐)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int i = 0;
int guess = 0;
int secret = 0;
srand(time(0));//给随机数设定种子
printf("请输入1到100之间的整数:");
scanf("%d",&secret);
for(;;)
{
guess = rand()%100;
i++;
// printf("%d\n",guess);
if(guess == secret)
{
printf("共猜了%d次",i);
break;
}
}
return 0;
}
2. 人猜(主)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(0));//给随机数设定种子
int secret = rand()%100 + 1;//要猜的数字
int gstime = 0;//猜的次数
printf("此地有一数\n其大不过百\n其小犹为一\n试问数几何\n\n");
while(1)
{
int guess = 0;
scanf("%d",&guess);
if(guess > secret)
{
gstime++;
printf("盈矣! (太大了)\n\n");
}
else if(guess < secret)
{
gstime++;
printf("虚矣! (太小了)\n\n");
}
else
{
gstime++;
printf("妙哉,共猜了%d次\n",gstime);
break;
}
}
if(gstime<=5)
printf("君乃举世无双!\n");
else if((gstime>5)&&(gstime<=10))
printf("君乃天地之才!\n");
else
printf("君乃童秀之才!\n");
return 0;
}
PS:猜数有技巧,二分法是最快的