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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
In
main()
the function, declare three integer variablesnum1
,num2
andnum3
, to store the three input integers. -
Use the object to assign the input three integers to variables , and
std::cin
through the input stream operator in sequence .>>
num1
num2
num3
-
Use the object to output the value of the variable
std::cout
through the output stream operator .<<
num2
-
After output,
std::endl
it is used to output a newline character to wrap the output result. -
return 0;
Indicates that the program ends normally. -
When running the program, enter three integers, separated by spaces.
-
The program will output the second input integer, which is
num2
the 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
In
main()
the function, declare two integer variablesx
andy
to store the two input integers. -
Use the object to assign the two input integers to the variables and in sequence
std::cin
through the input stream operator .>>
x
y
-
Evaluate an expression, use
3 * x + 2 * y
, and assign the result to a variableresult
. -
Use the object to output the value of the variable
std::cout
through the output stream operator .<<
result
-
After output,
std::endl
it is used to output a newline character to wrap the output result. -
return 0;
Indicates that the program ends normally. -
When running the program, enter two integers separated by spaces.
-
The program evaluates the expression
3x + 2y
and prints the result.
For example, for the sample input 2 3
, the program evaluates the expression 3 * 2 + 2 * 3
and 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
Include
<cctype>
the header file in order to usestd::isupper
the function to determine whether a character is an uppercase letter. -
In
main()
the function, declare a character variablech
to store the input characters. -
Use the object to assign the input characters to variables
std::cin
through the input stream operator .>>
ch
-
Use
std::isupper
the function to determinech
whether the variable is an uppercase letter. If it is an uppercase letter, the return value is true; otherwise, the return value is false. -
Based on the judgment results, use conditional statements
if
to output corresponding results. If it is an uppercase letter, output "YES"; otherwise, output "NO". -
After output,
std::endl
it is used to output a newline character to wrap the output result. -
return 0;
Indicates that the program ends normally. -
When running the program, enter a character.
-
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 K
whether 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
In
main()
the function, two integer variablesn
and are declaredk
to store the input integer number and single-digit value respectively. -
Use the object to assign the two input integers to the variables and in sequence
std::cin
through the input stream operator .>>
n
k
-
Declare an integer variable
sum
to store the cumulative sum of numbers that meet the condition, with an initial value of 0. -
Use to
for
loop over the input integers. -
Within the loop, declare an integer variable
num
to store the currently entered integer. -
Use the object to assign the currently input integer to the variable
std::cin
through the input stream operator .>>
num
-
Use conditional statements
if
to determinenum
whether the single digits of are equalk
. If equal,num
addsum
to . -
After the loop ends, output
sum
the value of the variable usingstd::cout
the object and output stream operators<<
. -
After output,
std::endl
it is used to output a newline character to wrap the output result. -
return 0;
Indicates that the program ends normally. -
When running the program, enter the integers n, k and n non-negative integers as required by the question.
-
The program will calculate the cumulative sum of numbers with k single digits and output the result.
For example, for the sample inputs 10 7
and 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:
-
First, include
<iostream>
the header files, which are the standard library for input and output. -
In
main()
the function, declare an integer variabledays
to store the entered number of days. -
Use the object to assign the input number of days to the variable
std::cin
through the input stream operator .>>
days
-
Declare an integer variable
coins
to store the number of gold coins obtained by the knight. The initial value is 0. -
Declare an integer variable
current_coins
to represent the current number of gold coins obtained continuously. The initial value is 1. -
Declare an integer variable
count
used to count the current number of consecutive days to obtain gold coins. The initial value is 0. -
Use
for
a loop to loop through each day, looping the variablei
from 1 to the entered number of days. -
In the loop, the current number of gold coins obtained continuously
current_coins
is accumulated into the variablecoins
. -
Increase the current count of consecutive days to obtain gold coins
count
by 1. -
Use conditional statements
if
to 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 becurrent_coins
increased by 1 andcount
reset to 0 at the same time. -
After the loop ends, output
coins
the value of the variable usingstd::cout
the object and output stream operators<<
. -
After output,
std::endl
it is used to output a newline character to wrap the output result. -
return 0;
Indicates that the program ends normally. -
When running the program, enter the number of days as required by the question.
-
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
.