Rumah >Java >javaTutorial >详细介绍java中最小公倍数与最大公约数
java中最小公倍数与最大公约数
import java.util.Scanner; /** * Created by Admin on 2017/3/26. */ public class test02 { public static int MinCommon(int a, int b) { int c, m = a * b; if(a<b){ int t=a; a=b; b=t; } if (a % b == 0) return a; else { while (b != 0) { c = a % b; a = b; b = c; } return m/a; } } public static void main(String[] args) { int a=1,b=1; Scanner scanner=new Scanner(System.in); while (a!=0||b!=0){ a=scanner.nextInt(); b=scanner.nextInt(); int result=MinCommon(a,b); System.out.println(result); System.out.println((a*b/result)); } } }
Atas ialah kandungan terperinci 详细介绍java中最小公倍数与最大公约数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!