C++ Practice [GESP2506 Level 1] Duty

GESP Level 1 Practice, Conditions and Logic, Beginner Difficulty.

  • CCF-GESP C++ Assessment Standards

    Hong Yang, WeChat Official Account: Hong Yang’s Programming Class CCF-GESP C++ Assessment Standards

Comprehensive Guide – [GESP2506 Level 1] Duty

Problem Requirements

Problem Description

Little Yang and Little Hong are on duty, responsible for cleaning the classroom. Little Yang is on duty every m days, and Little Hong is on duty every n days. Today they are both on duty together, how many days will it be until they are on duty together again?

Input Format

The first line contains a positive integer m, representing Little Yang’s duty cycle;

The second line contains a positive integer n, representing Little Hong’s duty cycle.

Output Format

A single line containing an integer, representing the minimum number of days until they are on duty together again.

Input and Output Examples

Input #1

4
6

Output #1

12

Input #2

6

Output #2

2

Notes/Tips

Data range: For all test cases, ensure 1 ≤ m, n ≤ 100.

Problem Analysis

Solution Approach

The essence of the problem is to find the least common multiple (LCM) of two numbers.

  • Little Yang is on duty every m days, and Little Hong is on duty every n days.

  • They start on the same day, and the next time they will be on duty together will be after m and n days, which is a common multiple.

  • The requirement for “at least how many days” is to find the least common multiple.

Mathematical formula:LCM(m,n)=(m×n)/GCD(m,n)

LCM(m,n)=m×nGCD(m,n)

Where GCD is the greatest common divisor.

Complexity Analysis:

  • Time complexity is O(log(min(m,n)))
  • Space complexity is O(log(min(m,n)))

Example Code

#include <bits/stdc++.h>using namespace std;// Using the Euclidean algorithm to find the greatest common divisorint gcd(int a, int b) {    if (a % b == 0)         return b;    return gcd(b, a % b);}int main() {    int m, n;    cin >> m >> n;  // Input m and n        int t = gcd(m, n);  // Calculate the greatest common divisor        // Least common multiple = m * n / greatest common divisor    cout << m * n / t;          return 0;}

Comprehensive Guide” series problems can be evaluated online atInformatics Olympiad Comprehensive Guide (C++ Version).

Leave a Comment