- package com.yuxi.day0923;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- printShow(125,200);
- }
- // 最大公约数
- public static int maxApproximate(int num1, int num2) {
- int max = 1;
- int min = (num1 < num2) ? num1 : num2;
- while (min >= 1) {
- if (num1 % min == 0 && num2 % min == 0) {
- max = min;
- break;
- }
- min--;
- }
- return max;
- }
- // 最小公倍数
- public static int minMultiple(int num1, int num2) {
- int min = 1;
- int max = (num1 > num2) ? num1 : num2;
- while (true) {
- if (max % num1 == 0 && max % num2 == 0) {
- min = max;
- break;
- }
- max++;
- }
- return min;
- }
- public static void printShow(int num1, int num2) {
- System.out.println(num1 + "与" + num2 + "的最大公约数是"
- + maxApproximate(num1, num2));
- System.out.println(num1 + "与" + num2 + "的最小公倍数是"
- + minMultiple(num1, num2));
- }
- }
复制代码
|
|