Analysis of the real questions of the Institute of Electronics C++ Programming Level Examination in May 2023 (Level 1)

Insert image description here

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

Question 1: Output the second integer

Input three integers and output the second input integer.
Time limit: 1000
Memory limit: 65536
input
There is only one line with three integers separated by a space. Integers are 32-bit signed integers.
The output
has only one line, one integer, which is the second integer input.
Sample input
123 456 789
Sample output
456

The following is the complete C++ code and parsing process to solve the problem:

#include <iostream>

int main() {
    
    
    int num1, num2, num3;
    std::cin >> num1 >> num2 >> num3;

    std::cout << num2 << std::endl;

    return 0;
}

The parsing process is as follows:

  1. First, include <iostream>the header files, which are the standard library for input and output.

  2. In main()the function, declare three integer variables num1, num2and num3, to store the three input integers.

  3. Use the object to assign the input three integers to variables , and std::cinthrough the input stream operator in sequence .>>num1num2num3

  4. Use the object to output the value of the variable std::coutthrough the output stream operator .<<num2

  5. After output, std::endlit is used to output a newline character to wrap the output result.

  6. return 0;Indicates that the program ends normally.

  7. When running the program, enter three integers, separated by spaces.

  8. The program will output the second input integer, which is num2the value of .

For example, for sample input 123 456 789, the program will output 456.

Question 2: Calculate the value of an expression

Given the values ​​of integers x and y, calculate the value of 3x+2y.
Time limit: 3000
Memory limit: 65536
input
One line contains 2 integers x, y, separated by spaces. The data ensures that the final calculation result is still within the range of integer representation.
Output:
Output the results according to the question requirements.
Sample input
2 3
Sample output
12

The following is the complete C++ code and parsing process to solve the problem:

#include <iostream>

int main() {
    
    
    int x, y;
    std::cin >> x >> y;

    int result = 3 * x + 2 * y;
    std::cout << result << std::endl;

    return 0;
}

The parsing process is as follows:

  1. First, include <iostream>the header files, which are the standard library for input and output.

  2. In main()the function, declare two integer variables xand yto store the two input integers.

  3. Use the object to assign the two input integers to the variables and in sequence std::cinthrough the input stream operator .>>xy

  4. Evaluate an expression, use 3 * x + 2 * y, and assign the result to a variable result.

  5. Use the object to output the value of the variable std::coutthrough the output stream operator .<<result

  6. After output, std::endlit is used to output a newline character to wrap the output result.

  7. return 0;Indicates that the program ends normally.

  8. When running the program, enter two integers separated by spaces.

  9. The program evaluates the expression 3x + 2yand prints the result.

For example, for the sample input 2 3, the program evaluates the expression 3 * 2 + 2 * 3and the output is 12.

Question 3: Judgment of capital letters

Enter a character and determine whether it is an English capital letter, that is, whether it is one of AZ.
Time limit: 1000
Memory limit: 65536
Enter
one character.
Output
If it is English capital letters, output YES, otherwise output NO.
Sample input
K
sample output
YES

The following is the complete C++ code and parsing process to solve the problem:

#include <iostream>
#include <cctype>

int main() {
    
    
    char ch;
    std::cin >> ch;

    if (std::isupper(ch)) {
    
    
        std::cout << "YES" << std::endl;
    } else {
    
    
        std::cout << "NO" << std::endl;
    }

    return 0;
}

The parsing process is as follows:

  1. First, include <iostream>the header files, which are the standard library for input and output.

  2. Include <cctype>the header file in order to use std::isupperthe function to determine whether a character is an uppercase letter.

  3. In main()the function, declare a character variable chto store the input characters.

  4. Use the object to assign the input characters to variables std::cinthrough the input stream operator .>>ch

  5. Use std::isupperthe function to determine chwhether the variable is an uppercase letter. If it is an uppercase letter, the return value is true; otherwise, the return value is false.

  6. Based on the judgment results, use conditional statements ifto output corresponding results. If it is an uppercase letter, output "YES"; otherwise, output "NO".

  7. After output, std::endlit is used to output a newline character to wrap the output result.

  8. return 0;Indicates that the program ends normally.

  9. When running the program, enter a character.

  10. The program will determine whether the entered characters are uppercase letters and output the corresponding results.

For example, for the sample input K, the program will determine Kwhether the character is an uppercase letter. Since it is an uppercase letter, it will output "YES".

Question 4: Accumulation of numbers that meet the conditions

There are n integers, and the numbers with k single digits are accumulated and summed.
Time limit: 1000
Memory limit: 65536
input
The first line contains two integers n and k, separated by spaces. (0 < n < 1000, 0 ≤ k ≤ 9) The second line contains n non-negative integers, separated by spaces, each number is not greater than 100000.
Output
Output the cumulative sum that meets the requirements of the question.
Sample input
10 7
2 5 7 17 11 18 4 27 1 7
Sample output
58

The following is the complete C++ code and parsing process to solve the problem:

#include <iostream>

int main() {
    
    
    int n, k;
    std::cin >> n >> k;

    int sum = 0;
    for (int i = 0; i < n; i++) {
    
    
        int num;
        std::cin >> num;

        if (num % 10 == k) {
    
    
            sum += num;
        }
    }

    std::cout << sum << std::endl;

    return 0;
}

The parsing process is as follows:

  1. First, include <iostream>the header files, which are the standard library for input and output.

  2. In main()the function, two integer variables nand are declared kto store the input integer number and single-digit value respectively.

  3. Use the object to assign the two input integers to the variables and in sequence std::cinthrough the input stream operator .>>nk

  4. Declare an integer variable sumto store the cumulative sum of numbers that meet the condition, with an initial value of 0.

  5. Use to forloop over the input integers.

  6. Within the loop, declare an integer variable numto store the currently entered integer.

  7. Use the object to assign the currently input integer to the variable std::cinthrough the input stream operator .>>num

  8. Use conditional statements ifto determine numwhether the single digits of are equal k. If equal, numadd sumto .

  9. After the loop ends, output sumthe value of the variable using std::coutthe object and output stream operators <<.

  10. After output, std::endlit is used to output a newline character to wrap the output result.

  11. return 0;Indicates that the program ends normally.

  12. When running the program, enter the integers n, k and n non-negative integers as required by the question.

  13. The program will calculate the cumulative sum of numbers with k single digits and output the result.

For example, for the sample inputs 10 7and 2 5 7 17 11 18 4 27 1 7, the program will calculate the cumulative sum of numbers with a single digit of 7, that is 7 + 17 + 27 + 7 = 58, the output result is 58.

Question 5: Gold coins

The king paid gold coins as wages to loyal knights. On the first day, the knight received one gold coin; for the next two days (the second and third days), he received two gold coins every day; for the next three days (the fourth, fifth, and sixth days), he received three gold coins every day. gold coins; in the next four days (seventh, eighth, ninth, and tenth days), four gold coins will be received every day... This wage payment model will continue like this: after receiving N gold coins every day for N consecutive days, The knight will receive N+1 gold coins every day for the next N+1 consecutive days (N is any positive integer).
You need to write a program that determines how many gold coins the knight earned in a given number of days starting from the first day.
Time limit: 10000
Memory limit: 65536
Input
An integer (range 1 to 10000) representing the number of days.
Output
The number of gold coins obtained by the knight.
Sample input
6
Sample output
14

The following is the complete C++ code and parsing process to solve the problem:

#include <iostream>

int main() {
    
    
    int days;
    std::cin >> days;

    int coins = 0;
    int current_coins = 1;
    int count = 0;

    for (int i = 1; i <= days; i++) {
    
    
        coins += current_coins;
        count++;

        if (count == current_coins) {
    
    
            current_coins++;
            count = 0;
        }
    }

    std::cout << coins << std::endl;

    return 0;
}

The parsing process is as follows:

  1. First, include <iostream>the header files, which are the standard library for input and output.

  2. In main()the function, declare an integer variable daysto store the entered number of days.

  3. Use the object to assign the input number of days to the variable std::cinthrough the input stream operator .>>days

  4. Declare an integer variable coinsto store the number of gold coins obtained by the knight. The initial value is 0.

  5. Declare an integer variable current_coinsto represent the current number of gold coins obtained continuously. The initial value is 1.

  6. Declare an integer variable countused to count the current number of consecutive days to obtain gold coins. The initial value is 0.

  7. Use fora loop to loop through each day, looping the variable ifrom 1 to the entered number of days.

  8. In the loop, the current number of gold coins obtained continuously current_coinsis accumulated into the variable coins.

  9. Increase the current count of consecutive days to obtain gold coins countby 1.

  10. Use conditional statements ifto determine whether the current number of consecutive days to obtain gold coins is equal to the current number of consecutive gold coins. If they are equal, it means that it is necessary to enter the next stage of continuously obtaining gold coins, and will be current_coinsincreased by 1 and countreset to 0 at the same time.

  11. After the loop ends, output coinsthe value of the variable using std::coutthe object and output stream operators <<.

  12. After output, std::endlit is used to output a newline character to wrap the output result.

  13. return 0;Indicates that the program ends normally.

  14. When running the program, enter the number of days as required by the question.

  15. The program will calculate the number of gold coins earned by the knight in a given number of days and output the result.

For example, for the sample input 6, the program will calculate the number of gold coins earned by the knight within 6 days starting from the first day according to the question requirements. According to the salary payment model given in the question, the number of gold coins obtained by the knight is 1 + 2 + 2 + 3 + 3 + 3 = 14, and the output result is 14.

Guess you like

Origin blog.csdn.net/gozhuyinglong/article/details/132868704
Recommended