Analysis of C/C++ (Level 1) real questions in December 2020 #中国 Electronics Society #National Youth Software Programming Level Examination

Insert image description here

C/C++ Programming (Levels 1~8) All real questions・Click here

Question 1: Character triangle

Description
Given a character, use it to construct an isosceles character triangle with a base length of 5 characters and a height of 3 characters.
Input
The input is only one line, containing one character.
Output
the isosceles triangle formed by this character, the base length is 5 characters, and the height is 3 characters.
Sample input
*
1Sample
output
  *
 ***
*****

You can write a program in C language to construct an isosceles character triangle with a base of 5 characters and a height of 3 characters. Here is sample code:

#include <stdio.h>

int main() {
    
    
    char ch;
    scanf("%c", &ch);

    printf("  %c\n", ch);
    printf(" %c%c%c\n", ch, ch, ch);
    printf("%c%c%c%c%c\n", ch, ch, ch, ch, ch);

    return 0;
}

In the main function, we declare the character variable ch and use the scanf function to read the input characters.

Next, we use the printf function to print the characters that make up the isosceles character triangle. On each line, we use spaces to control the position of the characters, and then use %c to format the character variable ch to print the characters. The first line has two spaces, the second line has one space, and the third line has no spaces.

Finally, we use the return 0; statement to end the program.

You can save the above code as a .c file, compile and run it with a C language compiler, input a character, and you will get the output result of the constructed isosceles character triangle.

Question 2: Calculate the value of (a+b)*(cb)

Description
Given 3 integers a, b, c, calculate the value of the expression (a+b)*(cb).
Input
: Enter only one line, including three integers a, b, c, separated by a space.
Output
Output one line, that is, the value of the expression
sample input
2 3 5
sample output
10

You can write a program in C language to evaluate the expression (a+b)*(cb). Here is sample code:

#include <stdio.h>

int main() {
    
    
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    int result = (a + b) * (c - b);

    printf("%d\n", result);

    return 0;
}

In the main function, we declare the integer variables a, b, and c, and use the scanf function to read the three input integers.

Next, we use an expression to assign the result of (a+b)*(cb) to the result variable.

Finally, we use the printf function to print the value of result.

You can save the above code as a .c file, compile and run it with a C language compiler, and input three integers to get the value output of the expression.

Question 3: Jingjing goes on a date

Jingjing’s friend Beibei asked Jingjing to go to the exhibition together next week, but Jingjing has classes on 1, 3, and 5 every week and must attend classes. Please help Jingjing judge whether she can accept Beibei’s invitation. If you can type YES ;If not, output NO. Note that YES and NO are both capital letters!
Input
There is a line in the input, the date Beibei invites Jingjing to see the exhibition, use numbers 1 to 7 to indicate from Monday to Sunday.
Output
The output has one line. If Jingjing can accept Beibei's invitation, output YES, otherwise, output NO.
Note that YES and NO are both capital letters!
Input sample
2
Output sample
YES

You can use C language to write a program to judge whether Jingjing can accept Beibei's invitation. Here is sample code:

#include <stdio.h>

int main() {
    
    
    int day;
    scanf("%d", &day);

    if (day == 1 || day == 3 || day == 5) {
    
    
        printf("NO\n");
    } else {
    
    
        printf("YES\n");
    }

    return 0;
}

In the main function, we declare the integer variable day and use the scanf function to read the input date.

Next, we use a conditional statement to determine whether the value of day is equal to 1, 3, or 5. If yes, it means Jingjing has a class that day and must attend, output "NO"; otherwise, it means Jingjing can accept the invitation, output "YES".

Finally, we print the results using the printf function.

You can save the above code as a .c file, compile and run it with a C language compiler, enter the date when Beibei invited Jingjing to the exhibition, and you will get the output result of whether to accept the invitation.

Question 4: Kakutani Conjecture

The so-called Kakutani conjecture means that for any positive integer, if it is an odd number, multiply it by 3 and add 1. If it is an even number, divide it by 2. The result obtained is then processed repeatedly according to the above rules, and finally 1 can always be obtained. For example, assuming the initial integer is 5, the calculation processes are 16, 8, 4, 2, and 1 respectively.
The program requires the input of an integer and outputs the process of obtaining 1 after processing.
Input
A positive integer N (N <= 2, 000, 000)
Output
The steps from the input integer to 1, each step is one line, and the calculation process is described in each part. The last line outputs "End". If the input is 1, "End" is output directly.
Sample input
5
Sample output
5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
End

You can write a program using C language to implement the Kakutani conjecture. Here is sample code:

#include <stdio.h>

void collatzConjecture(int num) {
    
    
    while (num != 1) {
    
    
        printf("%d", num);
        if (num % 2 == 0) {
    
    
            printf("/2=");
            num /= 2;
        } else {
    
    
            printf("*3+1=");
            num = num * 3 + 1;
        }
        printf("%d\n", num);
    }
    printf("End\n");
}

int main() {
    
    
    int num;
    scanf("%d", &num);

    collatzConjecture(num);

    return 0;
}

First, we define a function collatzConjecture to implement the calculation process of Kakutani conjecture. The parameter of the function is an integer num.

Inside the function, we use a while loop to determine whether num is equal to 1. If it is not equal to 1, proceed to the next calculation.

In the calculation process of each step, we output the current num value, and perform different calculations according to the parity of num. If num is even, we divide it by 2 and output the result; if num is odd, we multiply it by 3 and add 1, and output the result.

Finally, we output "End" after the loop ends to indicate the end of the calculation process.

In the main function, we declare the integer variable num and use the scanf function to read the input integer.

Then, we call the collatzConjecture function, passing in num as a parameter, to calculate the Kakutani conjecture.

You can save the above code as a .c file, compile and run it with a C language compiler, input a positive integer, and you will get the output result of the process of getting 1 after processing.

Question 5: Find the 3-digit number that meets the conditions

Write a program to find all 3-digit numbers that meet conditions 1 and 2 in ascending order. The conditions are: 1.
The number is a perfect square number
2. The number has at least 2 identical digits
. For example, 100 satisfies the above two conditions at the same time. conditions.
Input
Input a number n, and the size of n does not exceed the number of 3-digit numbers that actually meet the conditions.
Output
The output is the nth 3-digit number (in ascending order) that satisfies the conditions.
Sample input
1
Sample output
100

You can write a program using C language to find the 3-digit number that satisfies the condition. Here is sample code:

#include <stdio.h>
#include <math.h>

int isPerfectSquare(int num) {
    
    
    int root = sqrt(num);
    return (root * root == num);
}

int hasRepeatedDigits(int num) {
    
    
    int digits[10] = {
    
    0}; // 数字出现次数的统计数组

    while (num > 0) {
    
    
        int digit = num % 10;
        digits[digit]++;
        num /= 10;
    }

    for (int i = 0; i < 10; i++) {
    
    
        if (digits[i] >= 2) {
    
    
            return 1; // 至少有2位数字相同
        }
    }

    return 0;
}

int main() {
    
    
    int n;
    scanf("%d", &n);

    int count = 0; // 计数器
    int num = 100; // 从最小的3位数开始

    while (count < n) {
    
    
        if (isPerfectSquare(num) && hasRepeatedDigits(num)) {
    
    
            count++;
        }
        num++;
    }

    printf("%d\n", num-1);

    return 0;
}

In the main function, we declare the integer variable n and use the scanf function to read the input number n.

Next, we define two helper functions. The function isPerfectSquareis used to judge whether a number is a perfect square number, and the function hasRepeatedDigitsis used to judge whether a number has at least two digits the same.

In the main function, we use a while loop to traverse the 3-digit number, and use the counter count to record the number of numbers that meet the condition.

In the loop, we determine whether the current num satisfies condition 1 and condition 2. If satisfied, add 1 to the counter.

When the counter count reaches the input number n, the loop ends.

Finally, we output the nth 3-digit number (that is, num-1) that meets the condition.

You can save the above code as a .c file, compile and run it with a C language compiler, input a number n, and you will get the nth 3-digit output result that meets the conditions.

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132700632