importjava.util.Scanner;publicclassTest6{//欧几里德辗转相除法publicstaticintgcd(inta,intb){intr;while(b!=0){r=a%b;a=b;b=r;}returna;}//最小公倍数的另一种解法publicstaticintgcdtwo(inta,intb){while(a!=b){if(a>b){a=a-b;}else{b=b-a;}}returna;}//最大公约数publicstaticintlcm(inta,intb){returna*b/gcd(a,b);}publicstaticvoidmain(String[]args){Scannerinput=newScanner(System.in);System.out.println("请输入第一个数:");inta=input.nextInt();System.out.println("请输入第二个数:");intb=input.nextInt();/*if(a<b){a=a+b;b=a-b;a=a-b;}*/System.out.println(gcdtwo(a,b));System.out.println(lcm(a,b));}}
如果觉得《Java输入两个正整数m和n 求其最大公约数和最小公倍数。》对你有帮助,请点赞、收藏,并留下你的观点哦!