---
title: "java 求最大公约数和最小公倍数"
author: "showmo"
date: "2011-12-17T14:20:52Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-12-17T14:20:52Z"
id: 1314535
url: "https://showmo.iteye.com/blog/1314535"
---

```
public class MultipleTest
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        
        int divisor = getGreatestCommonDivisor(m, n);
        
        System.out.println("The Greatest Common Divisor: " + divisor + ".\n");
        System.out.println("The Lowest Common Multiple: " + m / divisor * n + ".\n");
    }
    
    // 欧几里德算法
    static int getGreatestCommonDivisor(int m, int n)
    {
        int r = m % n;
        
        while (r != 0) {
            m = n;
            n = r;
            
            r = m % n;
        }
        
        return n;
    }
}
```

 

目前，具我所知，用欧几里德算法求最大公约数，算是最简单的了，而用求出的最大公约数来计算最小公倍数，也就是一步计算的事儿。