本章主要讲解如何做一个简易的猜数游戏,分为用户猜数和系统猜数。
前置芝士:
系统想好一个在 [ 1 , 100 ] [1,100] [1,100] 之间的整数,由用户来猜数,而系统只能回答“过大”“过小”“正确”。
使用随机数来随机一个 [ 1 , 100 ] [1,100] [1,100] 的整数,猜测数初始设置为 − 1 -1 −1。
srand(time(0));
int x=-1,ans=rand()%100+1;
让系统讲清楚每次猜的数字的范围。
然后就直接让用户输入数字。
printf("I have a number from 1 to 100. Please have a guess: ");
scanf("%d",&x);
记一个变量 t m s tms tms,每次加一。
判断分为四种情况:
if(x<1||x>100) puts("The number is error.");
else if(x>ans) puts("The number is larger than my number!");
else if(x<ans) puts("The number is smaller than my number!");
else puts("Oh, you are right!");
外层的循环条件,只要 x ≠ a n s x\neq ans x=ans 时,就执行。
while(x!=ans)
{
...
}
输出 t m s tms tms 并终止。
printf("You guessed it %d times.",tms);
完整代码:
#include
using namespace std;
int main()
{
srand(time(0));
int x=-1,ans=rand()%100+1,tms=0;
while(x!=ans)
{
printf("I have a number from 1 to 100. Please have a guess: ");
scanf("%d",&x);
tms++;
if(x<1||x>100) puts("The number is error.");
else if(x>ans) puts("The number is larger than my number!");
else if(x<ans) puts("The number is smaller than my number!");
else puts("Oh, you are right!");
}
printf("You guessed it %d times.",tms);
return 0;
}
用户想好一个 [ 1 , 100 ] [1,100] [1,100] 范围的数,让系统猜。太大输入
L
,太小输入S
,正确输入R
。
有了上面的操作,我们让系统猜,写起来整体还是很简单的,但是要让系统聪明些。
先摆出程序框架:
#include
using namespace std;
int main()
{
srand(time(0));
puts("Please think a number from 1 to 100. And then I'll guess it.");
puts("If I guess right, you should say \"R\"(Right).");
puts("If my guess is too large, you should say \"L\"(Large).");
puts("If my guess is too small, you should say \"S\"(Small).");
puts("DON'T TELL A LIE!\n");
char c='\0';
int tms=0;
while(c!='R')
{
//...
printf("I guess the number is %d.Is it right(R, L or S)? ",/*...*/);
scanf("%c%*c",&c);
tms++;
if(c=='R') break;
//...
}
printf("I guess it %d times!",tms);
return 0;
}
系统只会瞎猜:
printf("I guess the number is %d.Is it right(R, L or S)? ",rand()%100+1);
显然,我们可以每一次缩小猜测范围。
char c='\0';
int tms=0,l=1,r=100;
while(c!='R')
{
int t=rand()%(r-l+1)+l;
printf("I guess the number is %d. Is it right(R, L or S)? ",t);
scanf("%c%*c",&c);
tms++;
if(c=='R') break;
if(c=='L') r=t;
if(c=='S') l=t;
}
Never gonna tell a lie and hurt you~
前面的程序判定不了我们在说谎,因此我们可以就 v2.0 添加一些东西(当 l ≥ r l\ge r l≥r 时必定不合法)。
char c='\0';
int tms=0,l=1,r=100;
while(c!='R')
{
int t=rand()%(r-l+1)+l;
printf("I guess the number is %d. Is it right(R, L or S)? ",t);
scanf("%c%*c",&c);
tms++;
if(c=='R') break;
if(c=='L') r=t;
if(c=='S') l=t;
if(l>=r)
{
puts("You told a lie!");
return 0;
}
}
没错,就是众望所归的二分。
改动这个即可:
int t=l+r>>1;
rand():要我有何用?
如果还是猜 50 50 50,效果:
计算机:惊不惊喜,意不意外!
But——《1 times》!
稍微改改即可,这里作者就不改了懒得改。
最终代码:
#include
using namespace std;
int main()
{
puts("Please think a number from 1 to 100. And then I'll guess it.");
puts("If I guess right, you should say \"R\"(Right).");
puts("If my guess is too large, you should say \"L\"(Large).");
puts("If my guess is too small, you should say \"S\"(Small).");
puts("DON'T TELL A LIE!\n");
char c='\0';
int tms=0,l=1,r=100;
while(c!='R')
{
int t=l+r>>1;
printf("I guess the number is %d. Is it right(R, L or S)? ",t);
scanf("%c%*c",&c);
tms++;
if(c=='R') break;
if(c=='L') r=t;
if(c=='S') l=t;
if(l>=r)
{
puts("You told a lie!");
return 0;
}
}
printf("I guess it %d times!",tms);
return 0;
}
本章详解了两种猜数游戏的实现,笔者在写的时候是一边写文章一边写代码一边截图的,也耗费了一定的精力。当然,这些不算什么,能让读者有所收获,才是最重要的!