輸入兩個正整數m和n,求其最大公約數和最小公倍數。

題目:輸入兩個正整數m和n,求其最大公約數和最小公倍數。java

 1 import java.util.Scanner;
 2 
 3 
 4 public class Algorithm_Game_06 {
 5     public static void main(String[] args) {
 6         Scanner s = new Scanner(System.in);
 7         int m = s.nextInt();
 8         int n = s.nextInt();
 9         System.out.println("最大公約數:"+f(m, n));
10         System.out.println("最小公倍數:"+m(m, n));
11     }
12     public static int f(int m,int n){
13         int c = m%n;
14         return c==0? n: f(n,c);
15     }
16     public static int m(int m,int n){
17          return (m*n/f(m,n));
18     }
19 }
相關文章
相關標籤/搜索