PTA C introductory exercise paragraph I - variables, expressions, branches, loops 7-21 - 7-25

Keywords: C pta

7-21 overspeed judgment (10 points)

Radar velocimeter simulating traffic police. Enter the vehicle speed. If the speed exceeds 60 mph, "speed" is displayed, otherwise, "OK" is displayed.

Input format:

The input gives a non negative integer no more than 500 in one line, that is, the vehicle speed measured by the radar.

Output format:

Output the speedometer display results in one line in the format: Speed: V - S, where V is the vehicle speed, S is either Speeding or OK.

Input example 1:

40

Output example 1:

Speed: 40 - OK

Input example 2:

75

Output example 2:

Speed: 75 - Speeding
#include <stdio.h>
int main(){
    int v;
    scanf("%d", &v);
    if(v > 60){
        printf("Speed: %d - Speeding", v);
    }else{
        printf("Speed: %d - OK", v);
    }
    return 0;
}

7-22 find a small ball with a balance (10 points)

Three balls A, B and C have the same size and shape, and one of them has A different weight from the other balls. Ask to find this different ball.

Input format:

Input: three positive integers are given in one line, and the order corresponds to the weight of balls A, B and C.

Output format:

Output the only different ball in one line.

Input example:

1 1 2

Output example:

C
#include <stdio.h>
int main(){
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    if(a == b){
        printf("C");
    }else {//(a != b)
        if(a == c){
            printf("B");
        }else{
            printf("A");
        }
    }
    return 0;
}

Calculation of residential water charge by sections 7-23 (10 points)

In order to encourage residents to save water, the water company adopts the method of step pricing according to water consumption. The water fee y (yuan) payable by residents is related to the monthly water consumption x (ton): when X does not exceed 15 tons, y=4x/3; After exceeding, y=2.5x − 17.5. Please write a program to calculate the water charge.

Input format:

The input gives a nonnegative real number x in one line.

Output format:

Output the water fee payable in one line, accurate to 2 decimal places.

Input example 1:

12

Output example 1:

16.00

Input example 2:

16

Output example 2:

22.50
#include <stdio.h>
int main(){
    float x, y;
    scanf("%f", &x);
    if(x <= 15){
        y = 4 * x / 3;
    }else{
        y = 2.5 * x - 17.5;
    }
    printf("%0.2f", y);
    return 0;
}

7-24 number guessing game (15 points)

The number guessing game is to make the game console randomly generate a positive integer within 100. The user inputs a number to guess it. You need to write a program to automatically compare it with the randomly generated guessed number, and prompt whether it is large ("Too big") or small ("Too small"), which means you have guessed. If you guess, end the program. The program also requires statistics on the number of guesses. If the number is guessed once, it will prompt "Bingo!"; If you can guess the number within 3 times, you will be prompted with "Lucky You!"; If you guess the number more than 3 times but within N (> 3) times (including the nth time), you will be prompted with "Good Guess!"; If you do not guess more than N times, you will be prompted with "Game Over" and end the program. If the user enters a negative number before reaching N times, it also outputs "Game Over" and ends the program.

Input format:

The first line of input gives two positive integers no more than 100, which are the random number generated by the game console and the maximum number of guesses N. Finally, each line gives a user's input until a negative number appears.

Output format:

Output the corresponding result of each guess in one line until the output of the guessed result or "Game Over" ends.

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 number, guess, times, gNumber;
    scanf("%d %d", &number, &guess);
    scanf("%d", &gNumber);
    times = 1;
    while(gNumber != number && gNumber >0){
        if(gNumber > number){
            printf("Too big\n");
        }else{
            printf("Too small\n");
        }
        times = times + 1;
        scanf("%d", &gNumber);
    }
    if(times == 1 && gNumber >0){
        printf("Bingo!\n");
    }else if(times <= 3 && gNumber >0){
        printf("Lucky You!\n"); 
    }else if(times <= guess && gNumber >0){
        printf("Good Guess!\n");
    }else{
        printf("Game Over\n");
    }
    return 0;
}

7-25 odd sum (15 points)

This problem requires calculating the sum of odd numbers in a given series of positive integers.

Input format:

Input gives a series of positive integers separated by spaces on one line. When zero or negative integer is read, it indicates the end of input, and the number should not be processed.

Output format:

Outputs the sum of odd numbers in a sequence of positive integers on one line.

Input example:

8 7 4 3 70 5 6 101 -1

Output example:

116
#include <stdio.h>
int main(){
    int number;
    int sum = 0;
    scanf("%d", &number);
    while(number > 0){
        if(number % 2 != 0){
            sum = sum + number;
        }
        scanf("%d", &number);
    }
    printf("%d", sum);
    return 0;
}

Posted by afatkidrunnin on Wed, 17 Nov 2021 22:08:31 -0800