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

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

 

13301๋ฒˆ: ํƒ€์ผ ์žฅ์‹๋ฌผ

๋Œ€๊ตฌ ๋‹ฌ์„ฑ๊ณต์›์— ๋†€๋Ÿฌ ์˜จ ์ง€์ˆ˜๋Š” ์ตœ๊ทผ์— ์ƒˆ๋กœ ๋งŒ๋“  ํƒ€์ผ ์žฅ์‹๋ฌผ์„ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค. ํƒ€์ผ ์žฅ์‹๋ฌผ์€ ์ •์‚ฌ๊ฐํ˜• ํƒ€์ผ์„ ๋ถ™์—ฌ ๋งŒ๋“  ํ˜•ํƒœ์˜€๋Š”๋ฐ, ํ•œ ๋ณ€์ด 1์ธ ์ •์‚ฌ๊ฐํ˜• ํƒ€์ผ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋งˆ์น˜ ์•ต๋ฌด์กฐ๊ฐœ

www.acmicpc.net


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

๐ŸŽจ Go

// https://www.acmicpc.net/problem/13301
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)

	if n == 1 {
		fmt.Fprintln(writer, 4)
		return
	} else if n == 2 {
		fmt.Fprintln(writer, 6)
		return
	}

	var sides = make([]int, n)
	sides = computeSides(sides)
	fmt.Fprintln(writer, getPerimeter(sides))
}

func computeSides(sides []int) []int {
	sides[0] = 1
	sides[1] = 1
	for i := 2; i < len(sides); i++ {
		sides[i] = sides[i-1] + sides[i-2]
	}
	return sides
}

func getPerimeter(sides []int) (perimeter int) {
	a := sides[len(sides)-1]
	b := sides[len(sides)-2]
	perimeter = a*4 + b*2
	return
}

๐ŸŽจ Python3

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

def compute_sides(sides):
    sides[0] = 1
    sides[1] = 1
    for i in range(2, len(sides)):
        sides[i] = sides[i-1] + sides[i-2]
    return sides

def get_perimeter(sides):
    a = sides[len(sides)-1]
    b = sides[len(sides)-2]
    perimeter = a*4 + b*2
    return perimeter

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    if n == 1:
        print(4)
        sys.exit()
    elif n == 2:
        print(6)
        sys.exit()

    sides = [0 for i in range(n)]
    sides = compute_sides(sides)
    print(get_perimeter(sides))
728x90
๋Œ“๊ธ€