Practice 4-6 Guess the number game (15 points)

Exercise 4-6
Number Guessing Game (15 points) The number guessing game is to make the game machine randomly generate a positive integer within 100. The user enters a number to make a guess. You need to write a program to automatically match it with the randomly generated guessed number Compare, and prompt whether it is big ("Too big") or small ("Too small"). Equality means guessed. If it is guessed, the program ends. The program also requires counting the number of guesses. If the number is guessed once, it will prompt "Bingo!"; if the number is guessed within 3 times, it will prompt "Lucky You!"; if it is more than 3 times but at N (>3) If the number is guessed within the Nth time (including the Nth), it will prompt "Good Guess!"; if it is not guessed more than N times, it will prompt "Game Over" and end the program. If the user inputs a negative number before reaching N times, "Game Over" is also output, and the program ends.

Input format:
Two positive integers not exceeding 100 are given in the first line of input, which are the random number generated by the game machine and the maximum number of guesses N. Finally, each line gives one user input until a negative number appears.

Output format:
Output the corresponding result of each guess in one line, until the output of the correct guess or "Game Over" is over.

Input example:
58 4
70
50
56
58
60
-2
Output example:
Too big
Too small
Too small
Good Guess!

#include<stdio.h>
int main()
{
    
    
    int a,b,c,i,flag=0;
    scanf("%d %d",&a,&b);
    for(i=1;i<=b;i++)
    {
    
    
        scanf("%d",&c);
        if(c==a)
        {
    
    
            flag=1;
            break;
        }
        else if(c<0)
        {
    
    
            break;
        }
        else if(c>a)
            printf("Too big\n");
        else if(c<a)
            printf("Too small\n");
    }
    if(flag==0)
        printf("Game Over");
    else if(i==1)
        printf("Bingo!");
    else if(i==2||i==3)
        printf("Lucky You!");
    else if(i>3)
        printf("Good Guess!");
}

Guess you like

Origin blog.csdn.net/ChaoYue_miku/article/details/114906392