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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
In
main()
the function, declare an integer variableN
to store the length of the integer sequence. -
Use the object to assign the length of the input integer sequence to the variable
std::cin
through the input stream operator .>>
N
-
Declare an
N
integer array of sizesequence
to store a sequence of integers. -
Use
for
a loop to iterate over the array, looping the variablei
from 0 toN-1
. -
In the loop, use the object to assign the input integer to each element of the array in turn
std::cin
through the input stream operator .>>
sequence
-
Declare an integer variable
x
to store the magnification factor. -
Use the object to assign the input magnification factor to the variable
std::cin
through the input stream operator .>>
x
-
Use
for
a loop to iterate over the array, looping the variablei
from 0 toN-1
. -
Within the loop,
sequence
each element of the array is multiplied by the magnification factorx
and the result is stored back into the corresponding array element. -
Using
std::cout
the object and output stream operators<<
, output each element of the array in a loopsequence
, separated by a space. -
After the output loop ends, use
std::endl
to output a newline character to wrap the output. -
return 0;
Indicates that the program ends normally. -
When running the program, enter the length of the integer sequence
N
, the integer sequence and the magnification factor as required by the questionx
. -
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 7
and 2
, the program will 1 5 7
amplify 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
Contains
<sstream>
the header file, which is the standard library for string stream operations. -
Contains
<vector>
the header file, which is the standard library for storing dynamic arrays. -
Contains
<algorithm>
header files, which are the standard library for algorithmic operations. -
In
main()
the function, declare a string variableinput
to store the input string. -
Use
std::getline()
the function to read a line of string from standard input and store itinput
in the variable . -
Declare an
std::istringstream
objectiss
that converts a stringinput
into a string stream. -
Declare a
std::vector<std::string>
containerwords
to store decomposed words. -
Declare a string variable
word
to temporarily store each word. -
Use
while
a loop to read words one by one through the string streamiss
and store themwords
in the container until the string streamiss
has read all the words. -
Use
std::reverse()
the function towords
reverse the order of words in the container. -
Use
for
a loop to iterate over the containerwords
, with the loop variablew
representing each word in the container. -
In a loop, to output words
w
, usestd::cout
the object and output stream operators<<
. -
After the output loop ends, use
std::endl
to output a newline character to wrap the output. -
return 0;
Indicates that the program ends normally. -
When running the program, enter a string as required by the question.
-
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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
Contains
<vector>
the header file, which is the standard library for storing dynamic arrays. -
In
main()
the function, declare two integer variablesm
andn
to store the number of rows and columns of the matrix. -
Use the object and assign the input row number and column number to the variables and respectively
std::cin
through the input stream operator .>>
m
n
m
n
-
Declare a two-dimensional integer vector
matrix
of size rows and columnsm
to store the elements of the matrix.n
-
Using two nested
for
loops, the loop variablei
goes from 0 tom-1
, and the loop variablej
goes from 0 ton-1
. -
In the loop, use the object to assign the input integer to each element of the matrix in turn
std::cin
through the input stream operator .>>
matrix
-
Declare an integer variable
sum
to store the sum of edge elements, with an initial value of 0. -
Using two nested
for
loops, the loop variablei
goes from 0 tom-1
, and the loop variablej
goes from 0 ton-1
. -
In the loop, use conditional judgment statements to judge whether the current element is located at the edge of the matrix, that is,
i
whether is 0 orm-1
, andj
whether is 0 orn-1
. -
If the current element is on the edge of the matrix, its value is accumulated into
sum
. -
Use
std::cout
the object and output stream operators<<
to outputsum
the value of the variable. -
After output, use
std::endl
to output a newline character to wrap the output. -
return 0;
Indicates that the program ends normally. -
When running the program, enter the number of rows
m
, columnsn
, and elements of the matrix as required by the question. -
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 1
and 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
Contains
<vector>
the header file, which is the standard library for storing dynamic arrays. -
Contains
<algorithm>
header files, which are the standard library for algorithmic operations. -
In
main()
the function, declare an integer variableN
to store the length of the sequence. -
Use the object to assign the length of the input to the variable
std::cin
through the input stream operator .>>
N
N
-
Declare an integer vector
numbers
of sizeN
that stores the elements of a sequence of positive integers. -
Using a
for
loop, the loop variablei
goes from 0 toN-1
. -
In the loop, use the object to assign the input integer to each element of the vector in turn
std::cin
through the input stream operator .>>
numbers
-
Declare a vector of integers
oddNumbers
to store the selected odd numbers. -
Using a
for
loop, the loop variablei
goes from 0 toN-1
. -
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. -
If the current element is odd, it is added to the vector
oddNumbers
. -
Use
std::sort()
the function tooddNumbers
sort the vector in ascending order. -
Using a
for
loop, the loop variable goesi
from 0 tooddNumbers
the size of decremented by 1. -
In the loop, use
std::cout
the object and the output stream operator<<
to outputoddNumbers[i]
the values of in turn. -
After each output, use a conditional statement to determine whether it is the last element. If not, output a comma
,
. -
After the output loop ends, use
std::endl
to output a newline character to wrap the output. -
return 0;
Indicates that the program ends normally. -
When running the program, enter the length of the sequence
N
and the elements of the sequence as required by the question. -
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, 9
and 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
Contains
<string>
the header file, which is the standard library for string manipulation. -
Declare a function
addRealNumbers()
to implement the logic of adding real numbers. This function accepts two parameters of string typenum1
andnum2
, representing two addends respectively. -
In
addRealNumbers()
the function, declare a string variableresult
to store the result of the addition. -
Declare an integer variable
carry
to record carry. -
Declare two integer variables
i
andj
, initialized to the index of the last character ofnum1
and , respectively.num2
-
Use a
while
loop, the loop condition isi
greater than or equal to 0 orj
greater than or equal to 0 orcarry
greater than 0. -
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.
-
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.
-
Convert the value of the current bit into a character and splice it in
result
front of . -
Update indexes
i
andj
, respectively, are decremented by 1. -
Returns
result
the result of the addition. -
In
main()
the function, declare two string variablesnum1
andnum2
to store the two input addends. -
Using the object, assign the two input addends to the variables and
std::cin
through the input stream operator .>>
num1
num2
-
Call
addRealNumbers()
the function, passing innum1
andnum2
, and assign the returned result to a variablesum
. -
Use
std::cout
the object and output stream operators<<
to outputsum
the value of the variable. -
After output, use
std::endl
to output a newline character to wrap the output. -
return 0;
Indicates that the program ends normally. -
When running the program, enter two real addends as required by the question.
-
The program will calculate the sum of two real numbers and print the result.
For example, for the sample inputs 0.111111111111111111111111111111
and 0.111111111111111111111111111111
, the program will calculate their sum as 0.222222222222222222222222222222
and then output the result as 0.222222222222222222222222222222
.