The greatest common divisor and least common multiple of C language - strange Interface Edition

Title Description

Given two positive integers x and y, find the greatest common divisor of two integers n and least common multiple.

Requirements (can not write a separate function gcd and lcm, does not meet the requirements will be counted wrong) use the following interfaces:

int get_gcdANDlcm(int x, int y, int *lcmptr);

The function returns the greatest common divisor, lcmptr should point to the least common multiple of object storage.

Entry

Comprising a plurality of sets of input data.

Each set of data separated by a space two positive integers x and y components. x and y are not more than 4,000.

Export

For each input, output and least common multiple of the greatest common divisor thereof, see the examples.

Sample input Copy

3 6
4 8

Sample output Copy

GCD=3,LCM=6
GCD=4,LCM=8

Code

#include <stdio.h>
#include<math.h> 
int get_gcdANDlcm(int x, int y, int *lcmptr)
{
	int arr[2];
	int t;
	
	arr[1]=x*y;
	
	if(x>y)
	{
		t=y;y=x;x=t;
	}
	while(x)
	{
		if(y%x==0)
		{
			arr[0]=x;
			break;
		}
		t=y;
		y=x;
		x=t%x;
	}
	int q;
	arr[1]=arr[1]/arr[0];
	q=arr[0];
	return q;
}
int main()
{	
	int x,g,y,a[2]={'\0'};
    while(scanf("%d %d",&x,&y)!=EOF)
    {
    	g=get_gcdANDlcm(x,y,a);
    	printf("GCD=%d,LCM=%d\n",g,x*y/g);
	}
    return 0;
}
Published 47 original articles · won praise 29 · views 1486

Guess you like

Origin blog.csdn.net/Qianzshuo/article/details/103758653