package 求两个数的公约数和公倍数;
import java.util.Scanner;  
public class MN
{
   
    public static void swap(int a,int b)//交换位置  
        {  
            int k;  
            if(a<b)  
            {  
                k=a;  
                a=b;  
                b=k;  
            }  
        }  
        static int shu(int a,int b)//循环寻找最大公约数  
        {  
            int k,y;  
            swap(a,b);  
            while(a%b!=0)  
            {  
                y=a%b;  
                a=b;  
                b=y;  
            }  
            return b;  
        }  
        static int shu2(int a,int b)//运用递归  
        {  
            int k,y;  
            swap(a,b);  
            y=a%b;  
            if(a%b==0)  
                return b;  
            else  
            {  
                a=b;  
                b=y;  
                return shu2(a,b);  
            }  
      
              
              
        }  
        static int beishu(int a,int b,int l)//求最小公倍数的公式为:a*b除以最大公约数  
        {  
            int k;  
            k=(a*b)/l;  
            return k;  
        }  
      
        public static void main(String[] args)
        {  
            int a,b,j,l,l2;  
            Scanner s=new Scanner(System.in);  
            System.out.println("请输入两个数:");  
            a=s.nextInt();  
            b=s.nextInt();  
            l=shu2(a,b);  
            System.out.println("循环求得最大公约数为:");  
            System.out.println(l);  
            System.out.println("递归求得最大公约数为:");  
            l2=shu2(a,b);  
            System.out.println(l2);  
            System.out.println("最小公倍数为:");  
            j=beishu(a,b,l);  
            System.out.println(j);  
        }  
      
}  
运算结果:

请输入两个数:
2 3
循环求得最大公约数为:
1
递归求得最大公约数为:
1
最小公倍数为:
6

 

posted on 2018-04-10 21:14  芽籽1997  阅读(274)  评论(0编辑  收藏  举报