Title Address: http://acm.nyist.net/JudgeOnline/problem.php?pid=40??? Describe
Xiao Ming is stumped by a problem and needs your help now. The problem is: give two positive integers to find out their greatest common divisor and least common multiple.
Input
The first line enters an integer n (0<n<=10000), which indicates that there are n sets of test data;
The subsequent n lines enter two integers i,j (0<i,j<=32767).
Output
Output greatest common divisor and least common multiple for each set of test data
Sample input
3
6 6
12 11
33 22
Sample output
6 6
1 132
11 66?? Code:
#include <stdio.h>
Calculate the least common multiple of a and B
static int calcommonmultiple (int a,int b,int maxcommondivisor);
Calculate the greatest common divisor of A and B
static int calcommondivisor (int a,int b);
int main ()
{
???? int readlen = 0;
???? scanf ("%d", &readlen);
???? GetChar ();
????
???? while (Readlen > 0)
???? {
????????? int a = 0;
????????? int b = 0;
????????? scanf ("%d%d", &a,&b);
????????? GetChar ();
?????????
????????? int divisornum = Calcommondivisor (A, b);
????????? int multiplenum = Calcommonmultiple (a,b,divisornum);
?????????
????????? printf ("%d%d\n", divisornum,multiplenum);
?????????
????????? --readlen;
???? }
????
???? return 0;
}
Calculate the least common multiple of a and B
static int calcommonmultiple (int a,int b,int maxcommondivisor)
{
???? return a*b/maxcommondivisor;
}
Calculate the greatest common divisor of A and B
static int calcommondivisor (int a,int b)
{
???? int maxnum = a>b?a:b;
???? int minnum = a<b?a:b;
????
???? int midresult = maxnum% Minnum;
???? while (Midresult! = 0)
???? {
????????? Maxnum = Minnum;
????????? Minnum = Midresult;
????????? Midresult = maxnum% Minnum;
???? }
????
???? return minnum;
} ? ? ? ??? Summary: The use of the method of the Division of the divide. Mathematics is also very important, especially in computer graphics and engineering software.
12-Language Primer -12-Convention number and common multiple