GESP Level 2 practice, nested loops and branches exercise, difficulty ★✮☆☆☆.
-
GESP Level 1 Practice Question List
-
GESP Level 1 Real Questions List
-
GESP Level 2 Practice Question List
-
GESP Level 2 Real Questions List
-
GESP Level 3 Practice Question List
-
GESP Level 3 Real Questions List
-
GESP Level 4 Practice Question List
-
GESP Level 4 Real Questions List
-
GESP Level 1-5 Syllabus Analysis
-
Essential Skills for GESP/CSP Programming
luogu-B3786 [Information and Future 2023] Lucky Numbers
Problem Requirements
Problem Description
If the sum of the digits at odd and even positions in the decimal representation of a number is the same, it is considered a lucky number by Xiao Xiao. For example:
Sum of odd positions and sum of even positions thus is not a lucky number;
Sum of odd positions and sum of even positions thus is a lucky number.
For the given and , Xiao Xiao hopes you can find the number of lucky numbers in .
Input Format
The input consists of a single line containing two integers and separated by a space.
Output Format
Output a single integer representing the number of lucky numbers in .
Sample Input #1
1 100
Sample Output #1
9
Sample Input #2
4096 65536
Sample Output #2
3454
Hints
- For the data of , it satisfies .
Problem Analysis
- First, we need to read the input range and .
- Next, we need to iterate through all numbers from to to check if each number is a lucky number.
- To check if a number is a lucky number, we need to separate it into odd and even positions and calculate their sums.
- If the sums of the odd and even positions are equal, then the number is a lucky number, and we need to count these lucky numbers.
- Finally, we output the count of lucky numbers.
In implementation, we can use a loop to iterate through all numbers from to and within that loop, use another loop to decompose the current number and calculate the sums of the odd and even positions.
Example Code
#include <iostream>
using namespace std;
int main() {
int a, b; // Define variables a and b to store the input range
cin >> a >> b; // Read the values of a and b from the input stream
int count = 0; // Initialize counter to count lucky numbers
for (int i = a; i <= b; i++) { // Iterate through all numbers from a to b
bool isOdd = true; // Initialize flag to mark if the current digit is odd
int oddSum = 0; // Initialize sum for odd positions
int evenSum = 0; // Initialize sum for even positions
int curNum = i; // Current number
while (curNum != 0) { // Continue loop while current number is not 0
int digit = curNum % 10; // Get the last digit of the current number
if (isOdd) {
oddSum += digit; // If current position is odd, add digit to odd sum
} else {
evenSum += digit; // If current position is even, add digit to even sum
}
isOdd = !isOdd; // Toggle flag for next position
curNum /= 10; // Remove the last digit from the current number
}
if (oddSum == evenSum) { // If odd sum equals even sum, current number is a lucky number
count++; // Increment counter
}
}
cout << count; // Output the value of the counter, which is the number of lucky numbers
return 0;
}
For detailed explanations of GESP levels, real questions, knowledge expansion, and practice lists, see:
[Pinned] [GESP] C++ Certification Learning Resource Summary
“luogu-” series problems can be evaluated online at Luogu Problem Bank.
“bcqm-” series problems can be evaluated online at Programming Enlightenment Problem Bank.