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

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

 

1145๋ฒˆ: ์ ์–ด๋„ ๋Œ€๋ถ€๋ถ„์˜ ๋ฐฐ์ˆ˜

์ฒซ์งธ ์ค„์— ๋‹ค์„ฏ ๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , ์„œ๋กœ ๋‹ค๋ฅธ ์ˆ˜์ด๋‹ค.

www.acmicpc.net


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

๐ŸŽจ Go

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

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

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

	var numbers = make([]int, 5)
	for i := 0; i < 5; i++ {
		fmt.Fscanf(reader, "%d ", &numbers[i])
	}

	var minLCM int = 10000000000
	for i := 0; i < 5; i++ {
		for j := i + 1; j < 5; j++ {
			lcmOfTwo := getLCM(numbers[i], numbers[j])
			for k := j + 1; k < 5; k++ {
				lcmOfThree := getLCM(lcmOfTwo, numbers[k])
				if lcmOfThree < minLCM {
					minLCM = lcmOfThree
				}
			}
		}
	}
	fmt.Fprintln(writer, minLCM)
}

func getGCD(first, second int) (gcd int) {
	if first < second {
		second, first = first, second
	}

	for second != 0 {
		first, second = second, first%second
	}
	return first
}

func getLCM(first, second int) (lcm int) {
	return first * second / getGCD(first, second)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1145
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__":
    numbers = list(map(int, sys.stdin.readline().split()))
    min_lcm = 10000000000
    for i in range(5):
        for j in range(i+1, 5):
            lcm_of_two = get_lcm(numbers[i], numbers[j])
            for k in range(j+1, 5):
                lcm_of_three = get_lcm(lcm_of_two, numbers[k])
                min_lcm = min(min_lcm, lcm_of_three)
    print(int(min_lcm))
728x90
๋Œ“๊ธ€