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!
     

    注:此题不需要自己生成随机数,可以理解成自己定义个随机数自己猜,并根据猜的次数及是否猜对进行相应的输出,输出比较多,需要点耐心

    #include "stdio.h"
    //#include "stdlib.h"
    //#include "time.h"
    int main()
    {
        int n,N,x,i,count=0;
        scanf("%d %d",&n,&N);
        scanf("%d",&x);
        if(x==n)
        {
            printf("Bingo!
    ");
            count++;
            goto out;
        }
        else if(x<0)
        {
                printf("Game Over
    ");
                count++;
                goto out;
        }
        else
        {
            for(i=1;i<4;i++)
            {            
                if(x<0)
                {
                    printf("Game Over
    ");
                    count++;
                    goto out;
                }
                else if(x>n)
                {
                    printf("Too big
    ");
                }
                else if(x<n)
                {
                    printf("Too small
    ");
                }
                else
                {
                    printf("Lucky You!
    ");
                    count++;
                    goto out;
                }
                count++;
                scanf("%d",&x);
            }
            for(i=4;i<=N;i++)
            {
                if(x<0)
                {
                    printf("Game Over
    ");
                    count++;
                    goto out;
                }
                else if(x>n)
                {
                    printf("Too big
    ");
                }
                else if(x<n)
                {
                    printf("Too small
    ");
                }
                else
                {
                    printf("Good Guess!
    ");
                    count++;
                    goto out;
                }
                count++;
                scanf("%d",&x);
            }
            printf("Game Over
    ");
        }
    out:    
        return 0;
    }
  • 相关阅读:
    数据结构(2)
    python数据结构(1)
    python 中__getitem__ 和 __iter__ 的区别
    python 中的 %s,%r,__str__,__repr__
    python中的zip
    python反射,单例模式
    类python中高级用法
    python中super与成员属性
    python 类与对象解析
    【其他】BootCDN
  • 原文地址:https://www.cnblogs.com/keepdoing/p/3946696.html
Copyright © 2011-2022 走看看