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

Insert image description here

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

Question 1: Digital Amplification

Given a sequence of integers and an amplification factor x, amplify each integer in the sequence x times and output it.
Time limit: 1000
Memory limit: 65536
The input
contains three lines: The first line is N, indicating the length of the integer sequence (N ≤ 100); the second line is N integers (not exceeding the integer range), separated by a space. ; The third line contains an integer (not exceeding the integer range), which is the specified integer x.
Output
N integers, which are the amplified sequences of the original sequence. The integers are separated by a space.
Sample input
3
1 5 7
2
Sample output
2 10 14

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

#include <iostream>

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

    int sequence[N];
    for (int i = 0; i < N; i++) {
    
    
        std::cin >> sequence[i];
    }

    int x;
    std::cin >> x;

    for (int i = 0; i < N; i++) {
    
    
        sequence[i] *= x;
        std::cout << sequence[i] << " ";
    }
    std::cout << 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 Nto store the length of the integer sequence.

  3. Use the object to assign the length of the input integer sequence to the variable std::cinthrough the input stream operator .>>N

  4. Declare an Ninteger array of size sequenceto store a sequence of integers.

  5. Use fora loop to iterate over the array, looping the variable ifrom 0 to N-1.

  6. In the loop, use the object to assign the input integer to each element of the array in turn std::cinthrough the input stream operator .>>sequence

  7. Declare an integer variable xto store the magnification factor.

  8. Use the object to assign the input magnification factor to the variable std::cinthrough the input stream operator .>>x

  9. Use fora loop to iterate over the array, looping the variable ifrom 0 to N-1.

  10. Within the loop, sequenceeach element of the array is multiplied by the magnification factor xand the result is stored back into the corresponding array element.

  11. Using std::coutthe object and output stream operators <<, output each element of the array in a loop sequence, separated by a space.

  12. After the output loop ends, use std::endlto output a newline character to wrap the output.

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

  14. When running the program, enter the length of the integer sequence N, the integer sequence and the magnification factor as required by the question x.

  15. The program will amplify each integer in the integer sequence and then output the amplified sequence.

For example, for the sample inputs 3, 1 5 7and 2, the program will 1 5 7amplify each integer in the integer sequence by 2 times to obtain 2 10 14, and then the output result is 2 10 14.

Question 2: Word inversion

Write a program to read a line of English (containing only letters and spaces, with words separated by single spaces), reverse the order of all words and output them, still separated by single spaces.
Time limit: 10000
Memory limit: 65536
Input
The input is a string (string length is at most 100).
Output
The output is a string sorted as required.
Sample input
I am a student
Sample output
student a am Ie

Here is a C++ code example that solves this problem:

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

int main() {
    
    
    std::string input;
    std::getline(std::cin, input);

    std::istringstream iss(input);
    std::vector<std::string> words;

    std::string word;
    while (iss >> word) {
    
    
        words.push_back(word);
    }

    std::reverse(words.begin(), words.end());

    for (const std::string& w : words) {
    
    
        std::cout << w << " ";
    }
    std::cout << 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. Contains <sstream>the header file, which is the standard library for string stream operations.

  3. Contains <vector>the header file, which is the standard library for storing dynamic arrays.

  4. Contains <algorithm>header files, which are the standard library for algorithmic operations.

  5. In main()the function, declare a string variable inputto store the input string.

  6. Use std::getline()the function to read a line of string from standard input and store it inputin the variable .

  7. Declare an std::istringstreamobject issthat converts a string inputinto a string stream.

  8. Declare a std::vector<std::string>container wordsto store decomposed words.

  9. Declare a string variable wordto temporarily store each word.

  10. Use whilea loop to read words one by one through the string stream issand store them wordsin the container until the string stream isshas read all the words.

  11. Use std::reverse()the function to wordsreverse the order of words in the container.

  12. Use fora loop to iterate over the container words, with the loop variable wrepresenting each word in the container.

  13. In a loop, to output words w, use std::coutthe object and output stream operators <<.

  14. After the output loop ends, use std::endlto output a newline character to wrap the output.

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

  16. When running the program, enter a string as required by the question.

  17. The program will decompose and reverse the input string and output the result.

For example, for the sample input "I am a student", the program will decompose the string into words "I", "am", "a", "student", and then perform inversion operation to obtain "student", "a", "am", "I", and the final output result is "student a am I".

Question 3: Calculate the sum of edge elements of a matrix

The so-called elements at the edge of the matrix are the elements in the first and last rows and the elements in the first and last columns.
Time limit: 10000
Memory limit: 65536
input
The first row is the number of rows m and the number of columns n of the matrix (m < 100, n < 100), separated by a space. In the next m lines of data input, each line contains n integers, separated by a space.
Output
Output the edge elements of the corresponding matrix and
sample input
3 3
3 4 1
3 7
1 2 0 1
sample output
15

Here is a C++ code example that solves this problem:

#include <iostream>
#include <vector>

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

    std::vector<std::vector<int>> matrix(m, std::vector<int>(n));

    for (int i = 0; i < m; i++) {
    
    
        for (int j = 0; j < n; j++) {
    
    
            std::cin >> matrix[i][j];
        }
    }

    int sum = 0;

    for (int i = 0; i < m; i++) {
    
    
        for (int j = 0; j < n; j++) {
    
    
            if (i == 0 || i == m - 1 || j == 0 || j == n - 1) {
    
    
                sum += matrix[i][j];
            }
        }
    }

    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. Contains <vector>the header file, which is the standard library for storing dynamic arrays.

  3. In main()the function, declare two integer variables mand nto store the number of rows and columns of the matrix.

  4. Use the object and assign the input row number and column number to the variables and respectively std::cinthrough the input stream operator .>>mnmn

  5. Declare a two-dimensional integer vector matrixof size rows and columnsm to store the elements of the matrix.n

  6. Using two nested forloops, the loop variable igoes from 0 to m-1, and the loop variable jgoes from 0 to n-1.

  7. In the loop, use the object to assign the input integer to each element of the matrix in turn std::cinthrough the input stream operator .>>matrix

  8. Declare an integer variable sumto store the sum of edge elements, with an initial value of 0.

  9. Using two nested forloops, the loop variable igoes from 0 to m-1, and the loop variable jgoes from 0 to n-1.

  10. In the loop, use conditional judgment statements to judge whether the current element is located at the edge of the matrix, that is, iwhether is 0 or m-1, and jwhether is 0 or n-1.

  11. If the current element is on the edge of the matrix, its value is accumulated into sum.

  12. Use std::coutthe object and output stream operators <<to output sumthe value of the variable.

  13. After output, use std::endlto output a newline character to wrap the output.

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

  15. When running the program, enter the number of rows m, columns n, and elements of the matrix as required by the question.

  16. The program will calculate the sum of the edge elements of the matrix and output the result.

For example, for the sample inputs 3 3, 3 4 1, 3 7 1and 2 0 1, the program will calculate the sum of the edge elements of the matrix as 15, and then output the result as 15.

Question 4: Odd single increasing sequence

Given a sequence of positive integers of length N (not greater than 500), please take out all the odd numbers and output them in ascending order.
Time limit: 1000
Memory limit: 65536
input
A total of 2 lines: The first line is N; the second line is N positive integers, separated by spaces.
Output
A sequence of odd numbers output in increasing order, separated by commas. The data is guaranteed to have at least one odd number.
Sample input
10
1 3 2 6 5 4 9 8 7 10
Sample output
1,3,5,7,9

Here is a C++ code example that solves this problem:

#include <iostream>
#include <vector>
#include <algorithm>

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

    std::vector<int> numbers(N);

    for (int i = 0; i < N; i++) {
    
    
        std::cin >> numbers[i];
    }

    std::vector<int> oddNumbers;

    for (int i = 0; i < N; i++) {
    
    
        if (numbers[i] % 2 != 0) {
    
    
            oddNumbers.push_back(numbers[i]);
        }
    }

    std::sort(oddNumbers.begin(), oddNumbers.end());

    for (int i = 0; i < oddNumbers.size(); i++) {
    
    
        std::cout << oddNumbers[i];
        if (i != oddNumbers.size() - 1) {
    
    
            std::cout << ",";
        }
    }

    std::cout << 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. Contains <vector>the header file, which is the standard library for storing dynamic arrays.

  3. Contains <algorithm>header files, which are the standard library for algorithmic operations.

  4. In main()the function, declare an integer variable Nto store the length of the sequence.

  5. Use the object to assign the length of the input to the variable std::cinthrough the input stream operator .>>NN

  6. Declare an integer vector numbersof size Nthat stores the elements of a sequence of positive integers.

  7. Using a forloop, the loop variable igoes from 0 to N-1.

  8. In the loop, use the object to assign the input integer to each element of the vector in turn std::cinthrough the input stream operator .>>numbers

  9. Declare a vector of integers oddNumbersto store the selected odd numbers.

  10. Using a forloop, the loop variable igoes from 0 to N-1.

  11. In the loop, use conditional judgment statements to judge whether the current element is an odd number, that is, numbers[i]whether it is an odd number.

  12. If the current element is odd, it is added to the vector oddNumbers.

  13. Use std::sort()the function to oddNumberssort the vector in ascending order.

  14. Using a forloop, the loop variable goes ifrom 0 to oddNumbersthe size of decremented by 1.

  15. In the loop, use std::coutthe object and the output stream operator <<to output oddNumbers[i]the values ​​of in turn.

  16. After each output, use a conditional statement to determine whether it is the last element. If not, output a comma ,.

  17. After the output loop ends, use std::endlto output a newline character to wrap the output.

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

  19. When running the program, enter the length of the sequence Nand the elements of the sequence as required by the question.

  20. The program will select odd numbers from the input sequence and output them in ascending order.

For example, for the sample input 10, 1 3 2 6 5 4 9 8 7 10, the program will select the odd numbers 1, 3, 5, 7, 9and sort them in ascending order, and then output the result as 1,3,5,7,9.

Question 5: Addition of real numbers

Find the sum of two real numbers added together.
The floating point numbers appearing in the input and output in the question have the following forms: P1P2…Pi.Q1Q2…Qj. For the integer part, P1P2...Pi is a non-negative integer and when the integer part is not 0, P1 is not equal to 0; for the decimal part, Qj is not equal to 0.
Time limit: 1000
Memory limit: 65536
Enter
2 lines, each line is an addend. The length of each addend cannot exceed 100.
Output
one line, the corresponding sum. The output is guaranteed to be a real number with a decimal part that is not 0.
Sample input
0.111111111111111111111111111111
0.1111111111111111111111111111111
Sample output
0.222222222222222222222222222222

Here is a C++ code example that solves this problem:

#include <iostream>
#include <string>

std::string addRealNumbers(const std::string& num1, const std::string& num2) {
    
    
    std::string result;
    int carry = 0;
    int i = num1.size() - 1;
    int j = num2.size() - 1;

    while (i >= 0 || j >= 0 || carry > 0) {
    
    
        int digit1 = i >= 0 ? num1[i] - '0' : 0;
        int digit2 = j >= 0 ? num2[j] - '0' : 0;
        int sum = digit1 + digit2 + carry;
        carry = sum / 10;
        int digit = sum % 10;
        result = std::to_string(digit) + result;
        i--;
        j--;
    }

    return result;
}

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

    std::string sum = addRealNumbers(num1, num2);

    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. Contains <string>the header file, which is the standard library for string manipulation.

  3. Declare a function addRealNumbers()to implement the logic of adding real numbers. This function accepts two parameters of string type num1and num2, representing two addends respectively.

  4. In addRealNumbers()the function, declare a string variable resultto store the result of the addition.

  5. Declare an integer variable carryto record carry.

  6. Declare two integer variables iand j, initialized to the index of the last character of num1and , respectively.num2

  7. Use a whileloop, the loop condition is igreater than or equal to 0 or jgreater than or equal to 0 or carrygreater than 0.

  8. In the loop, use a conditional judgment statement to determine whether the current index is valid. If it is valid, convert the character at the corresponding position into an integer, otherwise set it to 0.

  9. Calculate the sum of two numbers plus the carry, and divide the sum by 10 to get the value of the carry and current digit.

  10. Convert the value of the current bit into a character and splice it in resultfront of .

  11. Update indexes iand j, respectively, are decremented by 1.

  12. Returns resultthe result of the addition.

  13. In main()the function, declare two string variables num1and num2to store the two input addends.

  14. Using the object, assign the two input addends to the variables and std::cinthrough the input stream operator .>>num1num2

  15. Call addRealNumbers()the function, passing in num1and num2, and assign the returned result to a variable sum.

  16. Use std::coutthe object and output stream operators <<to output sumthe value of the variable.

  17. After output, use std::endlto output a newline character to wrap the output.

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

  19. When running the program, enter two real addends as required by the question.

  20. The program will calculate the sum of two real numbers and print the result.

For example, for the sample inputs 0.111111111111111111111111111111and 0.111111111111111111111111111111, the program will calculate their sum as 0.222222222222222222222222222222and then output the result as 0.222222222222222222222222222222.

Guess you like

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