GESP C++ Level 2 Practice: luogu-b3658 Mental Calculation Exercise

GESP Learning Resource List
Real Questions Practice Questions Syllabus Analysis
Level 1 Real Questions List Level 1 Practice Questions List Level 1-5 Syllabus Analysis
Level 2 Real Questions List Level 2 Practice Questions List Essential Skills for GESP/CSP Programming
Level 3 Real Questions List Level 3 Practice Questions List
Level 4 Real Questions List Level 4 Practice Questions List
Level 5 Real Questions List Level 5 Practice Questions List
CSP Learning Resource List
CSP-XL
2025 Liaoning CSP-XL Re-examination Real Questions Analysis

GESP Level 2 Practice, multiple nested branches / mathematical functions, difficulty ★✮☆☆☆.

luogu-B3658 [Language Monthly Competition 202209] Mental Calculation Exercise

Problem Requirements

Problem Description

In class, students often need to practice mental calculations.

The teacher posed a practice question: calculate the value of , please answer this question.

Where represents the largest integer not exceeding .

Hint:

In C++, the int type variables x, y, the statement <span>x/y</span> computes the value that rounds towards zero. For example:

int x = -3, y = 2;
cout &lt;&lt; x / y;

The above code will output -1.

<span>floor</span> function, which rounds down, for example:<span>floor(-1.5) = -2.0</span>

Input Format

Input consists of one line with two integers , guaranteed .

Output Format

Output one line with an integer, representing .

Sample Input #1

2 1

Sample Output #1

2

Sample Input #2

-3 2

Sample Output #2

-2

Data Range

For data, ; for other data, guaranteed ; for other data, guaranteed ; for data, .

Problem Analysis

To round down

  • (1) We can directly use the floor function;
  • (2) If calculating manually: we need to compute divided by and convert the result to an integer. Specifically, if the result is positive, we take the integer part directly; if the result is negative, we need to check if is divisible by . If it is, we take the integer part directly; if not, we need to round down, for example, rounding down to . (We can first convert to a positive number, then cast to int and add 1, then convert back to negative.)

Example Code

Code 1: Using floor function

#include &lt;cmath&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    int x, y; // Define two integer variables x and y
    cin &gt;&gt; x &gt;&gt; y; // Read the values of x and y from input stream
    double ans = (double)x / (double)y; // Calculate the result of x divided by y and convert to float
    cout &lt;&lt; floor(ans); // Output the integer part of the result
    return 0; // Return 0, indicating successful execution
}

Code 2: Manually implementing rounding towards zero

#include &lt;cmath&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    int x, y; // Define two integer variables x and y
    cin &gt;&gt; x &gt;&gt; y; // Read the values of x and y from input stream
    double temp = (double)x / y; // Calculate the result of x divided by y and convert to float
    int result; // Define integer variable result
    if (temp &gt;= 0) {
        result = (int)temp; // If the result is non-negative, take the integer part directly
    } else {
        if (x % y == 0) {
            result = (int)temp; // If x is divisible by y, take the integer part directly
        } else {
            result = ((int)abs(temp) + 1) * -1; // If x is not divisible by y, round towards zero
        }
    }
    cout &lt;&lt; result; // Output the result
    return 0; // Return 0, indicating successful execution
}

[Recommended] Mobile version recommended:[GESP] C++ Certification Learning Resource Summary

[Recommended] Desktop version recommended: GESP/CSP examination materials website:https://wiki.coderli.com/ Dictionary-style resource organization, categorized for easy access.

GESP C++ Level 2 Practice: luogu-b3658 Mental Calculation Exercise

luogu-” series questions can be evaluated online atLuogu Question Bank.

bcqm-” series questions can be evaluated online atProgramming Enlightenment Question Bank.

Leave a Comment