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

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

 

2748๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 2

๋ฌธ์ œ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0๊ณผ 1๋กœ ์‹œ์ž‘ํ•œ๋‹ค. 0๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0์ด๊ณ , 1๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ 2๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š” ๋ฐ”๋กœ ์•ž ๋‘ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ ํ•ฉ์ด ๋œ๋‹ค. ์ด๋ฅผ ์‹์œผ๋กœ ์จ๋ณด๋ฉด Fn = Fn-1 + Fn-2 (n>=2)๏ฟฝ๏ฟฝ

www.acmicpc.net


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

๐ŸŽจ Go

package main

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

func main() {
	var n int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	
	var fibMap = make(map[int]int, n)
	fibMap[0] = 0
	fibMap[1] = 1
	fmt.Fprintln(writer, fibonacci(fibMap, n))
}

func fibonacci(fibMap map[int]int, n int) (result int) {
	for i:=2; i<=n; i++ {
		fibMap[i] = fibMap[i-1]+fibMap[i-2]
	}
	return fibMap[n]
}
728x90
๋Œ“๊ธ€