13.题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
最大公约数:使用辗转相除法
最小公倍数=两个正整数相乘除以最大公约数
图片来源与百度百科
/**
*
*/
package com.gem.demo.day03_practice;
import java.util.Scanner;
/**
*
* Description:13.题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
* 辗转相除法求最大公约数
* 最小公倍数=两个数相乘除以最大公约数。
*
* @author HadwinLing
*
* @date 2020年1月10日下午7:49:33
*
* @version 0.0.1
*
*/
public class practice13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数");
int num1 = input.nextInt();
System.out.println("请输入一个数");
int num2 = input.nextInt();
int max = max(num1,num2);
System.out.println("max="+max);
System.out.println("min="+(num1*num2/max));
}
public static int max(int a,int b) {
int temp;
if(a<b) {
temp=b;
b=a;
a=temp;
}
int Remainder ;//余数
while((Remainder=a%b)!=0) {
a=b;
b = a%b;
}
return b;
}
}