首页 > 代码库 > java每日小算法(6)

java每日小算法(6)

/*【程序6】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。  */
package test;
public class test {
    //最大公约数
     public static int commonisor(int n, int m) {
         int max = (n>=m)?n:m;
         int min = (n>=m)?m:n;
         int r = max % min;
         while(r != 0)
         {
             max = min;
             min = r;
             r = max % min;
         }
         return min;
     }
        //最小公倍数
     public static int commonmilt(int n, int m) {
         int commonisor = commonisor(n,m);
         int result = n / commonisor * m;
         return result;
     }
 public static void main(String[] args) {
     long a = System.currentTimeMillis();
     int n = 81;
     int m = 9;
     System.out.println("Commonisor of "+n+" and "+m+" is: "+commonisor(n,m));
     System.out.println("Commonmilt of "+n+" and "+m+" is: "+commonmilt(n,m));
     System.out.println(System.currentTimeMillis() - a);
 }
}