본문 바로가기
프로그램

[파이썬] 문제 : n x n의 사각형에서 1,0,1,0 순으로 사각형 만들기

by 오디세이99 2022. 11. 26.
728x90
반응형

n = int(input(''))                          
lst = [[1 for j in range(n)] for i in range(n)]     # 모두 1로 채움

s_idx = 1                                             # 시작 인덱스. 0으로 채움
e_idx = n-2                                           # 종료 인덱스. 0으로 채움
while s_idx < n/2:
    for i in range(s_idx, e_idx+1):
        for j in range(s_idx, e_idx+1):
            if j != s_idx-1 and j != e_idx+1 and i == s_idx:
                lst[i][j] = 0
            elif j != s_idx-1 and j != e_idx+1 and i == e_idx:
                lst[i][j] = 0
            elif j != s_idx-1 and j != e_idx+1 and i != s_idx-1 and i != e_idx+1 and j == s_idx:
                lst[i][j] = 0
            elif j != s_idx-1 and j != e_idx+1 and i != s_idx-1 and i != e_idx+1 and j == e_idx:
                lst[i][j] = 0
    s_idx = s_idx + 2      # 사각형 왼쪽에서 안으로 이동
    e_idx = e_idx - 2      # 사각형 오른쪽에서 안으로 이동

for row in lst:            # 사격향으로 보기. 결과는 이것과 print까지 비고(#)로 처리
    print(row)
print()

print(lst)                  #  실제 출력

728x90
반응형

댓글