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

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

 

2193๋ฒˆ: ์ด์นœ์ˆ˜

0๊ณผ 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ˆ˜๋ฅผ ์ด์ง„์ˆ˜๋ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ์ด์ง„์ˆ˜ ์ค‘ ํŠน๋ณ„ํ•œ ์„ฑ์งˆ์„ ๊ฐ–๋Š” ๊ฒƒ๋“ค์ด ์žˆ๋Š”๋ฐ, ์ด๋“ค์„ ์ด์นœ์ˆ˜(pinary number)๋ผ ํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” ๋‹ค์Œ์˜ ์„ฑ์งˆ์„ ๋งŒ์กฑํ•œ๋‹ค. ์ด์นœ์ˆ˜๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/2193
/*
N = 1์ผ๋•Œ count = 1
1

N = 2์ผ๋•Œ count = 1
10

N = 3์ผ๋•Œ count = 2
100
101

N = 4์ผ๋•Œ count = 3
1000
1010
1001

N = 5์ผ๋•Œ count = 5
10000
10100
10101
10010
10001

N = 6์ผ๋•Œ count = 8
100000
101000
101010
101001
100100
100101
100010
100001

...
*/
package main

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

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

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

	var count = make([]int, n+1)
	for i := 1; i < n+1; i++ {
		if i < 2 {
			count[i] = 1
		} else {
			count[i] = count[i-1] + count[i-2]
		}
	}
	fmt.Fprintln(writer, count[n])
}

๐ŸŽจ Python3

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

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    count = [0 for i in range(n+1)]
    for i in range(1, n+1):
        if i < 2:
            count[i] = 1
        else:
            count[i] = count[i-1] + count[i-2]
    print(count[n])
728x90
๋Œ“๊ธ€