ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1934๋ฒˆ: ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

๋‘ ์ž์—ฐ์ˆ˜ A์™€ B์— ๋Œ€ํ•ด์„œ, A์˜ ๋ฐฐ์ˆ˜์ด๋ฉด์„œ B์˜ ๋ฐฐ์ˆ˜์ธ ์ž์—ฐ์ˆ˜๋ฅผ A์™€ B์˜ ๊ณต๋ฐฐ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Ÿฐ ๊ณต๋ฐฐ์ˆ˜ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 6๊ณผ 15์˜ ๊ณต๋ฐฐ์ˆ˜๋Š” 30, 60, 90๋“ฑ์ด ์žˆ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1934
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanln(reader, &t)

	for i := 0; i < t; i++ {
		var a, b int
		fmt.Fscanln(reader, &a, &b)
		fmt.Fprintln(writer, getLCM(a, b))
	}

}

// ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜ ๊ตฌํ•˜๊ธฐ (์œ ํด๋ฆฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์šฉ)
func getLCM(first, second int) (lcm int) {
	return first * second / getGCD(first, second) // ๋‘ ์ˆ˜์˜ ๊ณฑ ๋‚˜๋ˆ„๊ธฐ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜
}

// ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜ ๊ตฌํ•˜๊ธฐ (์œ ํด๋ฆฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด์šฉ)
func getGCD(first, second int) (gcd int) {
	if first < second { // fn์— ํฐ ๊ฐ’์„ ์˜ค๊ฒŒ ํ•˜๊ธฐ
		second, first = first, second
	}

	for second != 0 { // second๊ฐ€ 0์ด ๋ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
		first, second = second, first%second
	}
	return first
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1934
import sys

def get_gcd(first, second):
    if first < second:
        second, first = first, second
    while second != 0:
        first, second = second, first%second
    return first
    
def get_lcm(first, second):
    return first*second / get_gcd(first, second)

if __name__ == "__main__":
    t = int(sys.stdin.readline())

    for i in range(t):
        a, b = list(map(int, sys.stdin.readline().split()))
        print(int(get_lcm(a, b)))
728x90
๋Œ“๊ธ€