zoukankan      html  css  js  c++  java
  • 04-4. 猜数字游戏(15)

    猜数字游戏是令系统随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,则提示“Game Over”,并结束程序。如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序。

    输入格式:

    输入第一行中给出2个不超过100的正整数,分别是系统产生的随机数、以及猜测的最大次数N。随后每行给出一个用户的输入,直到出现负数为止。

    输出格式:

    在一行中输出每次猜测相应的结果,直到输出猜对的结果或“Game Over”则结束。

    输入样例:

    58 4
    70
    50
    56
    58
    60
    -2
    

    输出样例:

    Too big
    Too small
    Too small
    Good Guess!
    
     1 #include <stdio.h>
     2  
     3 int main()
     4 {
     5     int number, n;
     6     scanf("%d %d", &number, &n);
     7     int i;
     8     int cnt = 0;
     9     int inp;
    10     for(i = 1; i < 100; i++)
    11     {
    12         scanf("%d", &inp);
    13         cnt++;
    14         if(inp < 0) {
    15             printf("Game Over");
    16             break;
    17         }
    18         if(inp == number) {
    19             if(cnt == 1) {
    20                 printf("Bingo!");break;
    21             }
    22             else if(cnt == 2 || cnt == 3) {
    23                 printf("Lucky You!");break;
    24             }
    25             else if(cnt > 3 && cnt <= n) {
    26                 printf("Good Guess!");break;
    27             }
    28             else if(cnt > n) {
    29                 printf("Game Over");break;
    30             }
    31         }
    32         else if(inp > number) {
    33                 printf("Too big
    ");
    34         }
    35         else if(inp < number) {
    36                 printf("Too small
    ");    
    37         }
    38     }
    39     
    40     return 0;
    41 }
  • 相关阅读:
    node
    github
    [模块] pdf转图片-pdf2image
    python 15 自定义模块 随机数 时间模块
    python 14 装饰器
    python 13 内置函数II 匿名函数 闭包
    python 12 生成器 列表推导式 内置函数I
    python 11 函数名 迭代器
    python 10 形参角度 名称空间 加载顺序
    python 09 函数参数初识
  • 原文地址:https://www.cnblogs.com/aexin/p/3871631.html
Copyright © 2011-2022 走看看