首页 > 编程语言 > 详细

java每日小算法(6)

时间:2014-05-27 04:02:56      阅读:364      评论:0      收藏:0      [点我收藏+]
/*【程序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);
 }
}


java每日小算法(6),布布扣,bubuko.com

java每日小算法(6)

原文:http://gomic.blog.51cto.com/8689134/1412582

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!