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

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

 

17212๋ฒˆ: ๋‹ฌ๋‚˜๋ผ ํ† ๋ผ๋ฅผ ์œ„ํ•œ ๊ตฌ๋งค๋Œ€๊ธˆ ์ง€๋ถˆ ๋„์šฐ๋ฏธ

๋‹ฌ๋‚˜๋ผ ํ† ๋ผ๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” ํ™”ํ๋Š” ๋™์ „๋ฟ์ด๋‹ค. ๋™์ „์˜ ์ข…๋ฅ˜๋Š” 1์›, 2์›, 5์›, 7์› ์ด๋ ‡๊ฒŒ 4์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๋ฌผ๊ฑด์„ ์‚ฌ๊ณ  ๋™์ „์œผ๋กœ ๊ณ„์‚ฐ์„ ํ•˜๋Š”๋ฐ ๋™์ „์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ์ง€๋ถˆํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์€ 

www.acmicpc.net


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

๐ŸŽจ Go

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

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

var (
	dp    []int
	coins = []int{7, 5, 2, 1}
)

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

	var n int
	fmt.Fscanln(reader, &n)
	dp = make([]int, n+1)
	for i := 1; i < n+1; i++ {
		dp[i] = 100000
	}
	for i := 1; i < n+1; i++ {
		for _, coin := range coins {
			if i-coin >= 0 && dp[i-coin]+1 < dp[i] {
				dp[i] = dp[i-coin] + 1
			}
		}
	}
	fmt.Fprintln(writer, dp[n])
}

๐ŸŽจ Python3

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

dp = []
coins = [7, 5, 2, 1]

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    dp = [100000 for _ in range(n+1)]
    dp[0] = 0
    for i in range(1, n+1):
        for coin in coins:
            if i-coin >= 0 and dp[i-coin]+1 < dp[i]:
                dp[i] = dp[i-coin] + 1
    print(dp[n])
728x90
๋Œ“๊ธ€