1
2
3 import java.io.*;
4
5 public class GCDLCM //GreatestCommonDivisor and LeastCommonMultiple(最大公约数和最小公倍数)
6 {
7 public int greatestCD( int m, int n)
8 {
9 int max = 0 , gcd = 1 ;
10 if (m > n)
11 max = m;
12 else
13 max = n;
14 for ( int i = 1 ; i <= max; i++)
15 {
16 if (m % i == 0 && n % i == 0 )
17 {
18 if (i > gcd)
19 gcd = i;
20 }
21 }
22 return gcd;
23 }
24
25 public static void main(String[] args)
26 {
27 GCDLCM gcdlcm = new GCDLCM();
28
29 String s = "" , t = "" ;
30 int gcd = 1 , lcm = 1 , m, n;
31
32
33 try
34 {
35 BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
36 BufferedReader in1 = new BufferedReader( new InputStreamReader(System.in));
37 s = in.readLine();
38 t = in1.readLine();
39 }
40 catch (IOException e)
41 {}
42
43 m = Integer.parseInt(s);
44 n = Integer.parseInt(t);
45
46 //求最大公约数
47 gcd = gcdlcm.greatestCD(m, n);
48 //求最小公倍数
49 lcm = m * n / gcd;
50
51 System.out.println(m + "和" + n + "最大公约数为:" + gcd);
52 System.out.println(m + "和" + n + "最小公倍数为:" + lcm);
53 }
54 }
2
3 import java.io.*;
4
5 public class GCDLCM //GreatestCommonDivisor and LeastCommonMultiple(最大公约数和最小公倍数)
6 {
7 public int greatestCD( int m, int n)
8 {
9 int max = 0 , gcd = 1 ;
10 if (m > n)
11 max = m;
12 else
13 max = n;
14 for ( int i = 1 ; i <= max; i++)
15 {
16 if (m % i == 0 && n % i == 0 )
17 {
18 if (i > gcd)
19 gcd = i;
20 }
21 }
22 return gcd;
23 }
24
25 public static void main(String[] args)
26 {
27 GCDLCM gcdlcm = new GCDLCM();
28
29 String s = "" , t = "" ;
30 int gcd = 1 , lcm = 1 , m, n;
31
32
33 try
34 {
35 BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
36 BufferedReader in1 = new BufferedReader( new InputStreamReader(System.in));
37 s = in.readLine();
38 t = in1.readLine();
39 }
40 catch (IOException e)
41 {}
42
43 m = Integer.parseInt(s);
44 n = Integer.parseInt(t);
45
46 //求最大公约数
47 gcd = gcdlcm.greatestCD(m, n);
48 //求最小公倍数
49 lcm = m * n / gcd;
50
51 System.out.println(m + "和" + n + "最大公约数为:" + gcd);
52 System.out.println(m + "和" + n + "最小公倍数为:" + lcm);
53 }
54 }