标签:初始 col 退出 max img 猜数字 include std ima
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4
5 int main() {
6 // num1:随机数, num2:猜测数字, count:次数
7 //minn:最小范围, maxn:最大范围
8 int num1, num2, count, minn, maxn;
9 //时间种子
10 srand(time (NULL));
11 printf("请输入随机数范围,最大32767,[0 ~ ");
12 scanf("%d", &maxn);
13 //对最大值+1取模,确定范围
14 num1 = rand()%(maxn+1);
15 //初始化猜测次数
16 minn = count = 0;
17 printf("已随机产生一个0~%d的数字, 按 ^z 退出\n", maxn);
18
19 //Ctrl + z 再回车即退出循环
20 while(~scanf("%d", &num2) ) {
21 count++;
22 //对比并确定答案范围
23 if(num2 > num1) {
24 printf("你输入的%d太大了\n", num2);
25 maxn = num2 - 1;
26 }
27 if(num2 < num1) {
28 printf("你输入的%d太小了\n", num2);
29 minn = num2 + 1;
30 }
31 if(num2 == num1) {
32 printf("终于被你猜对了! ");
33 break;
34 }
35 printf("答案的范围是[%d~%d]: ", minn, maxn);
36 }
37 printf("你总共猜了%d次\n", count);
38
39 return 0;
40 }
标签:初始 col 退出 max img 猜数字 include std ima
原文地址:https://www.cnblogs.com/Lunix-touch/p/12200518.html