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

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

 

9461๋ฒˆ: ํŒŒ๋„๋ฐ˜ ์ˆ˜์—ด

๋ฌธ์ œ ์˜ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์‚ผ๊ฐํ˜•์ด ๋‚˜์„  ๋ชจ์–‘์œผ๋กœ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ์ฒซ ์‚ผ๊ฐํ˜•์€ ์ •์‚ผ๊ฐํ˜•์œผ๋กœ ๋ณ€์˜ ๊ธธ์ด๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ •์‚ผ๊ฐํ˜•์„ ๊ณ„์† ์ถ”๊ฐ€ํ•œ๋‹ค. ๋‚˜์„ ์—์„œ ๊ฐ€์žฅ ๊ธด ๏ฟฝ

www.acmicpc.net


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

๐ŸŽจ Go

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	var t int
	fmt.Fscanln(reader, &t)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	for i :=0; i<t; i++ {
		var n int
		fmt.Fscanln(reader, &n)
		lengths := calculate(n)
		fmt.Fprintf(writer, "%d\n", lengths[n-1])
	}
}

func calculate(n int) (lengths []int) {
	lengths = append(lengths, 1, 1, 1)
	if n <= 3 {
		return
	}
	for i := 3; i < n+1 ; i++ {
		lengths = append(lengths, lengths[i-2]+ lengths[i-3])
	}
	return
}

๐ŸŽจ Python3

import sys

def calculate(n):
    lengths = [1, 1, 1]
    if n <= 3:
        return lengths
    for i in range(3, n+1):
        lengths.append(lengths[i-2]+lengths[i-3])
    return lengths
        
if __name__ == "__main__":
    t = int(sys.stdin.readline())
    for i in range(t):
        n = int(sys.stdin.readline())
        lengths = calculate(n)
        print(lengths[n-1])
728x90
๋Œ“๊ธ€