SDUT-1131 C/C++ Training 1---Greatest Common Divisor and Least Common Multiple (JAVA*)

C/C++ training 1---the greatest common divisor and the least common multiple

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

Enter two positive integers and find their greatest common divisor and least common multiple.

Input

Enter two positive integers separated by spaces.

Data is guaranteed to be in the int range.

Output

The first line outputs the greatest common divisor;
the second line outputs the least common multiple.

The answer is guaranteed to be in the int range.

Sample Input

64 48

Sample Output

16
192

Hint

Source

import java.util. *;

class f {
	int n;
	int m;

	int gcd(int nn, int mm) {
		if (mm == 0)
			return nn;
		else
			return gcd(mm, nn % mm);
	}

	int lcm(int nn,int mm)
	{
		int ans=this.gcd(nn, mm);
		return nn*mm/ans;
	}
}

public class Main {

	public static void main(String args[]) {
		Scanner cin = new Scanner(System.in);
		f x=new f();
		int n = cin.nextInt ();
		int m = cin.nextInt ();
		int ans1=x.gcd(n, m);
		int ans2=x.lcm(n,m);
		System.out.println(ans1);
		System.out.println(ans2);
		cin.close();
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603503&siteId=291194637