網站首頁 編程語言 正文
0. 引言
本章主要講解如何做一個簡易的猜數游戲,分為用戶猜數和系統猜數。
前置芝士:
「C++小游戲教程」基本技巧(1)——隨機化
1. 用戶猜數
系統想好一個在 [ 1 , 100 ] [1,100][1,100] 之間的整數,由用戶來猜數,而系統只能回答“過大”“過小”“正確”。
1-1. 設置答案數與猜測數
使用隨機數來隨機一個 [ 1 , 100 ] [1,100][1,100] 的整數,猜測數初始設置為 ? 1 -1?1。
srand(time(0));
int x=-1,ans=rand()%100+1;
1-2. 系統說明要求與讀入數字
讓系統講清楚每次猜的數字的范圍。
然后就直接讓用戶輸入數字。
printf("I have a number from 1 to 100. Please have a guess: ");
scanf("%d",&x);
1-3. 累計猜測次數與判斷數字
記一個變量tms,每次加一。
判斷分為四種情況:
1.當x?[1,100] 時,拋出錯誤。
if(x<1||x>100) puts("The number is error.");
2.當x>ans 時,說明數字過大,輸出。
else if(x>ans) puts("The number is larger than my number!");
3.當x<ans 時,類似,數字過小,輸出。
else if(x<ans) puts("The number is smaller than my number!");
4.當x=ans 時,正確,提示輸出。
else puts("Oh, you are right!");
外層的循環條件,只要x≠ans時,就執行。
while(x!=ans)
{
...
}
1-4. 輸出猜測次數
輸出tms 并終止。
printf("You guessed it %d times.",tms);
完整代碼:
#include<bits/stdc++.h>
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;
}
效果:
2. 系統猜數,但是是進化史
用戶想好一個[1,100] 范圍的數,讓系統猜。太大輸入L,太小輸入S,正確輸入R。
有了上面的操作,我們讓系統猜,寫起來整體還是很簡單的,但是要讓系統聰明些。
先擺出程序框架:
#include<bits/stdc++.h>
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;
}
2-1. 代碼 v1.0——我會瞎猜!
系統只會瞎猜:
printf("I guess the number is %d.Is it right(R, L or S)? ",rand()%100+1);
效果顯著:
為系統堅持不懈的精神點贊!
2-2. 代碼 v2.0——我會縮小范圍!
顯然,我們可以每一次縮小猜測范圍。
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;
}
效率提升了:
系統:我是最快的!
2-3. 代碼 v3.0——我會清白!
Never gonna tell a lie and hurt you~
前面的程序判定不了我們在說謊,因此我們可以就 v2.0 添加一些東西(當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;
}
}
聰明多了:
2-4. 代碼 v4.0——我會二分!
沒錯,就是眾望所歸的二分。
改動這個即可:
int t=l+r>>1;
rand():要我有何用?
如果還是猜50,效果:
計算機:驚不驚喜,意不意外!
But——《1 times》!
稍微改改即可,這里作者就不改了懶得改。
最終代碼:
#include<bits/stdc++.h>
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;
}
原文鏈接:https://blog.csdn.net/Leo_Chenjy/article/details/127703281
相關推薦
- 2022-03-30 用Python判斷奇偶數示例_python
- 2022-09-28 OpenCV(python)版實現文本分割之水平投影法_python
- 2022-07-06 Nginx的mirror指令示例配置_nginx
- 2022-08-14 C#使用Data?Annotations進行手動數據驗證_C#教程
- 2022-11-07 React?全面解析excel文件_React
- 2022-05-29 Docker鏡像與容器的導入導出操作實踐_docker
- 2022-04-24 python使用技巧-文件讀寫_python
- 2022-09-08 pytorch?tensor內所有元素相乘實例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支