GESP Level 2 exercise, involving multiple conditions and nested loops, difficulty ★✮☆☆☆.
luogu-b2081 Numbers Not Related to 7
Problem Requirements
Problem Description
A positive integer is considered related to 7 if it can be divided by 7, or if any digit in its decimal representation is 7. The task is to find the sum of the squares of all positive integers less than or equal to n that are not related to 7.
Input Format
The input consists of a single line containing a positive integer n.
Output Format
The output is a single line containing an integer, which is the sum of the squares of all positive integers less than or equal to n that are not related to 7.
Sample Input #1
21
Sample Output #1
2336
Problem Analysis
This is a simple mathematical problem that requires us to find all positive integers less than or equal to n that are not related to 7, and calculate their sum of squares. We can use a loop to iterate through all numbers less than or equal to n and check if each number is not related to 7. If it is not related, we add its square to the answer. Finally, we output the answer.
In solving this problem, the student utilized the constraints of the input data range, simplifying the method to determine numbers not related to 7 by only checking single-digit and two-digit cases.
Example Code
Method 1
Utilizes the constraints of the input data range, direct method
#include <iostream>
using namespace std; // Use standard namespace
int main() {
int n; // Define variable n
cin >> n; // Read the value of n from input stream
int ans = 0; // Initialize answer variable
for (int i = 1; i <= n; i++) { // Iterate from 1 to n
if (i % 7 != 0 && i % 10 != 7 && i / 10 % 10 != 7) { // Check if i is not related to 7
ans += i * i; // If not related to 7, add the square of i to the answer
}
}
cout << ans; // Output the answer
return 0;
}
Method 2
Utilizes similar logic from previous exercise b2082, extended to a general method for any case
#include <iostream>
using namespace std;
int main() {
int n; // Define variable n
cin >> n; // Read the value of n from input stream
int ans = 0; // Initialize answer variable
for (int i = 1; i <= n; i++) { // Iterate from 1 to n
int d = i, num; // Initialize variable d as i, num to store each digit
bool flag = false; // Initialize flag variable to check if i is not related to 7
if (i % 7 == 0) { // If i is divisible by 7, then i is related to 7
flag = true;
continue; // Skip current iteration, continue to next number
}
while (d != 0) { // Loop until d becomes 0
num = d % 10; // Get the last digit of d
if (num == 7) { // If the last digit is 7, then i is related to 7
flag = true;
break; // Exit the loop
}
d /= 10; // Remove the last digit from d
}
if (!flag) { // If i is not related to 7
ans += i * i; // Add the square of i to the answer
}
}
cout << ans; // Output the answer
return 0;
}
For more details on GESP levels, past exam questions, knowledge expansion, and practice lists, see:
【Pinned】【GESP】C++ Certification Learning Resource Compilation
“luogu-” series problems can be evaluated online at Luogu Problem Bank.
“bcqm-” series problems can be evaluated online at Programming Enlightenment Problem Bank.