求最小公倍数以及最大公因子

Sicily 1623. Sixth Grade Math


Time Limit: 1 secs, Memory Limit: 32 MB

Description

In sixth grade, students are presented with different ways to calculate the Least Common Multiple (LCM) and the Greatest Common Factor (GCF) of two integers. The LCM of two integers a and b is the smallest positive integer that is a multiple of both a and b . The GCF of two non-zero integers a and b is the largest positive integer that divides both a and b without remainder.

Input

The first line of input contains a single integer N , (1≤N≤1000) which is the number of data sets that follow. Each data set consists of a single line of input containing two positive integers, a and b , (1≤a, b≤1000) separated by a space.

Output

For each data set, you should generate one line of output with the following values: The data set number as a decimal integer (start counting at one), a space, the LCM, a space, and the GCF.

Sample Input

3
5 10
7 23
42 56
Sample Output

1 10 5
2 161 1
3 168 14


( ̄旦 ̄;) Just do it!

#include 

using namespace std;

int GCD(int num1, int num2)
{
    if (num1 % num2 != 0)
    {
        return GCD(num2, num1 % num2);
    }
    else
        return num2;
}

int LCM(int num1, int num2)
{
    return num1 * num2 / GCD(num1, num2);
}

int main()
{
    int T, num1, num2;
    cin >> T;
    for (int i = 1; i <= T; i++)
    {
        cin >> num1 >> num2;
        cout << i << " " << LCM(num1, num2)
                  << " " << GCD(num1, num2) 
             << endl;
    }
    return 0;
}

你可能感兴趣的:(编程语言,C,C++)