Little Monkey Programming C++ | Greatest Common Divisor of Intervals

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

The little monkey recently learned the basic knowledge of the greatest common divisor in mathematics class. In order to strengthen the little monkey's understanding of the greatest common divisor, Teacher H assigned the little monkey a homework question:

The little monkey needs   to choose any two integers  x and y between LR ( Lx < yR ), and  the greatest common divisor of x and y is required to be the largest .

Please help the little monkey complete this task.

【enter】

A line contains two  integers L and R.

【Output】

A line containing an integer representing the answer.

【Input sample】

23 29

【Output sample】

4

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int L, R;
    cin >> L >> R;
    for (int i=R/2; i>=1; i--) {
        if (R/i - (L-1)/i >= 2) {
            cout << i;
            return 0;
        }
    }
    return 0;
}

【operation result】

23 29
4

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/133914624