Programming_prob/Codesignal
[Intro] shapeArea
YongArtist
2020. 7. 18. 16:10
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
그림에서 보면 알 수 있듯이 n이 주어졌을 때, 모양의 개수를 반환하는 문제이다.
import sys
sys.setrecursionlimit(15000)
def shapeArea(n):
if n==0:
return 1
else:
return 4*(n-1)+shapeArea(n-1)
3년 전에도 이와 비슷한 문제를 풀었던 기억이 어렴풋이 나는데, 어떻게 풀었는지 기억이 나질 않아서 재귀함수를 통해 수열을 적용해 연산해서 풀었다.
def shapeArea(n):
return n**2 + (n-1)**2
위 코드는 다른사람이 푼 코드다. 이제 뭐가 부족한지 알겠는가.