Notice
Recent Posts
Recent Comments
Link
Star_project
n 소용돌이 수 | 달팽이 문제 본문
[COS PRO 1급] 1회 5번 - n 소용돌이 수 | 달팽이 문제
#You may use import as below.
#import math
def solution(n):
# Write code here.
a=[ [0]*n for _ in range(n)]
dir=0
loop=n
r, c=0, -1
num=0
dr=[0, 1, 0 ,-1]
dc=[1, 0, -1, 0]
while num<n*n:
for _ in range(loop):
r+=dr[dir]
c+=dc[dir]
num+=1
a[r][c]=num
dir=(dir+1)%4
if dir%2 : loop-=1
answer=0
for x in range(n):
answer+=a[x][x]
return answer
#The following is code to output testcase.
n1 = 3
ret1 = solution(n1)
#Press Run button to receive output.
print("Solution: return value of the function is", ret1, ".")
n2 = 2
ret2 = solution(n2)
#Press Run button to receive output.
print("Solution: return value of the function is", ret2, ".")
Solution: return value of the function is 15 .
Solution: return value of the function is 4 .
참고 영상: https://www.youtube.com/watch?v=3fVqkLwgoPA&list=PLnp1rUgG4UVbZ9t778bKYSH9G5UhllEIE&index=6
'코딩테스트 > Python' 카테고리의 다른 글
2. 자료구조와 알고리즘 입문 03.K번째 큰 수 (0) | 2022.07.11 |
---|---|
알파벳 숫자로 변환/바꾸기 in python (0) | 2022.06.09 |
코딩테스트 4. 이분 탐색(결정알고리즘) & 그리디 알고리즘 (0) | 2022.06.07 |
코딩테스트 3. 탐색 & 시뮬레이션 (2) (0) | 2022.06.06 |
코딩테스트 3. 탐색 & 시뮬레이션 (1) (0) | 2022.06.03 |