首页 > 其他 > 详细

寻找最大公因数和最小公倍数

时间:2018-04-10 23:38:49      阅读:233      评论:0      收藏:0      [点我收藏+]

package 寻找公因数;
import java.util.Scanner;
public class 寻找公因数 {
 public static void main(String[] args) { 
        Scanner in = new Scanner(System.in); 
        System.out.print("input x :"); 
        int x = in.nextInt(); 
        System.out.print("input y :"); 
        int y = in.nextInt(); 
         
        int z = Method(x,y);  
        System.out.println("最大公因数 : "+z); 
        System.out.println("最小公倍数 : "+(x*y/z)); 
 }
         //计算公约数

//辗转相除法
          public static int Method(int x,int y){ 
              int a,b,c; 
              a=x; 
              b=y; 
              while(b!=0){ 
                  c=a%b; 
                  a=b; 
                  b=c; 
              } 
              return a; 
          } 
        //求公倍数 
          public static int multiple(int x,int y){ 
              int z; 
              for(z=x;;z++){ 
                  if(z%x==0&&z%y==0){ 
                      break; 
                  } 
              } 
              return z; 
          } 
 
 }


  
       
     

input x :3
input y :5
辗转相除法:
最大公因数 : 1
最小公倍数 : 15

 

寻找最大公因数和最小公倍数

原文:https://www.cnblogs.com/infinite14/p/8783387.html

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