import java.util.Scanner; public class Test6 { //欧几里德 辗转相除法 public static int gcd(int a,int b) { int r ; while(b != 0) { r = a % b ; a = b; b = r; } return a; } //最小公倍数的另一种解法 public static int gcdtwo(int a,int b){ while (a != b) { if (a > b ) { a = a -b; } else { b = b - a; } } return a; } //最大公约数 public static int lcm(int a,int b) { return a * b /gcd(a,b); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入第一个数:"); int a = input.nextInt(); System.out.println("请输入第二个数:"); int b = 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)); } }
本文出自 “橙子的成长记录” 博客,请务必保留此出处http://azhome.blog.51cto.com/9306775/1546770
原文地址:http://azhome.blog.51cto.com/9306775/1546770