12-Language Primer -12-Convention number and common multiple

Source: Internet
Author: User
Tags greatest common divisor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.