java最小公倍数
- //输入两个数求最小公倍数
- 用a*b/最大公约数
public class smileCommon { public static void main(String[] args){ Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int t = a * b / gets(); } public static int gets(int a ,int b){ while(b != 0){ int t = a % b; a = b; b = t; } return a; }
//最小公倍数肯定比两个数都大 public class smileCommon { 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(); int max = a > b ? a : b; while(max > 0) { if (max % a == 0 && max % b == 0) { break; } max++; } System.out.println("最大公倍数是:" + max); } }