求最小公倍数

正整数A和正整数B 的最小公倍数是指能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。

输入描述:

输入两个正整数A和B。

输出描述:

输出A和B的最小公倍数。

示例1

输入
5 7
输出
35

示例2

输入
2 4
输出
4

Java 编程

package cn.net.javapub.demo2.demo;

/**
 * @author: shiyuwang
 * @url: http://javapub.net.cn
 */

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String str = reader.readLine();
        String[] strarray = str.split(" ");
        int x = Integer.valueOf(strarray[0]);
        int y = Integer.valueOf(strarray[1]);
        if (x == y) {
            System.out.println(x);
        } else if (x > y) {
            for (int i = 1; i <= y; i++) {
                if (i * x % y == 0) {
                    System.out.println(i * x);
                    break;
                }
            }
        } else {
            for (int i = 1; i <= x; i++) {
                if (i * y % x == 0) {
                    System.out.println(i * y);
                    break;
                }
            }
        }


    }

}

展示效果:

华为OD机试 - 求最小公倍数 (Java 2024 E卷 100分)_算法