Simple division (C++) kkmd66

difficulty

Find the greatest common divisor of the numerator and denominator and divide

Description:

Back then, Ding Xinyi – when the current leader Ding was still in elementary school, the beautiful teacher wanted to see if leader Ding had the potential to engage in ACM, so she came up with a programming question to test leader Ding. The question is as follows, input two numbers, such as a, b, and a, b are integers greater than 0, a<=b, output the result of dividing a by b, the result is represented by a true fraction, that is, the simplest fraction. When the teacher just finished the question, the sharp teacher Ding immediately KOed the question within two minutes, and won a praise from the teacher. Now, smart, can you surpass the leader Ding and become a new generation leader? come on! Try to think, try to do, coding now, programming your future!

Input:

Enter two numbers a, b, ending with EOF.

Output:

Output the result of dividing a by b, and the result is expressed as a fraction. For example, 1 divided by 2 is 1/2, 2/4=1/2, if a is equal to b, then output 1.

Sample Input:

1 2
2 4

Sample Output:

1/2
1/2

#include <iostream>

using namespace std;

/**
 * kkmd66
 * @return
 */

int main() {
    
    

    //接收
    int m_a, m_b;
    while (cin >> m_a >> m_b) {
    
    

        //中转赋值
        int a = m_a, b = m_b;
        int c = a % b;

        //找最大公约数
        while (c != 0) {
    
    
            a = b;
            b = c;
            c = a % b;
        }

        //最大公约数
        c = b;

        //输出
        if (m_a == m_b)
            cout << "1" << endl;
        if (m_a != m_b)
            cout << m_a / c << "/" << m_b / c << endl;
    }

    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324029973&siteId=291194637