Skip to content

Latest commit

 

History

History
305 lines (231 loc) · 6.36 KB

200204_List2.md

File metadata and controls

305 lines (231 loc) · 6.36 KB

List (2)

2020/02/04 (화)


C:\Users\multicampus\algo\01_List_II\workshop

1. 파리 퇴치 풀이

  • 2차원 배열 풀이방법

  • 전체를 돌면서 파리채 범위 합을 리스트에 넣어줌

  • 반복 횟수가 중요 (N-M+1)

# swea 2001_flies.py
import sys
sys.stdin = open('input.txt')

T = int(input())

for tc in range(1, T+1):
    N, M = map(int, input().split())
    d = [list(map(int, input().split())) for _ in range(N)]

    # 파리채가 순회하는 횟수
    kill = []
    for i in range(N-M+1):
        for j in range(N-M+1):
            s = 0 # 파리 총합
            for k in range(M):
                s += sum(d[i+k][j:j+M])    # 이 부분이 이해하는것이 제일 중요
            kill.append(s)
    
    print('#{} {}'.format(tc, max(kill)))

2. Sum 문제 풀이

  • 2차원 배열 풀이
  • 동일한 최댓값이 있을 경우 하나의 값만 출력 -> max() 하나만 출력함
# swea 1209_sum.py
import sys
sys.stdin = open('input.txt')

T = 10

for tc in range(1, T+1):
    c = 100
    n = int(input())
    # 2차원 배열 생성 100x100행렬
    d = [list(map(int, input().split())) for _ in range(c)]

    m = 0

    # 행 우선 순회
    for i in range(c):
        s = 0
        for j in range(c):
            s += d[i][j]
        if s > m:
            m = s

    # print(i, j)
    # i, j 를 또 쓰는것은 위에서 반복문이 다 끝났기 때문에 사용가능    

    # 열 우선 순회
    for i in range(c):
        s = 0
        for j in range(c):
            s += d[j][i]
        if s > m:
            m = s

    # 대각선 합 구하기 (\)
    s = 0
    for i in range(c):
        s += d[i][i]
    if s > m:
        m = s

    # 대각선 합 구하기 (/)
    s = 0
    for i in range(c):
        s += d[i][c-1-i]
        # s += d[i][-(i+1)]
    if s > m:
        m = s
    
    print(m)

3. IM기출 ( 스위치 켜고 끄기 )

  • 남자일때 리스트의 특정인덱스 들의 배수들만 컨트롤 할 수 있는지 여부

  • 여자일때 좌우.. 컨트롤 가능한지..

# 백준 스위치 끄고 켜기 문제
# IM난이도 수준
import sys
sys.stdin = open('input.txt')

'''
n : 스위치 수
data : 스위치 상태
s : 학생 수
sex : 성별
num : 받은 숫자
'''
n = int(input())
data = list(map(int, input().split()))
s = int(input())
for _ in range(s):
    sex, num = map(int, input().split())
    # 남자일 때
    if sex == 1:
        a = 1
        while a <= len(data)//num :
            if data[a*num-1] == 1:
                data[a*num-1] = 0
            else :
                data[a*num-1] = 1
            a += 1
            
    # 여자일 때
    else:
        # data[num-1] 기준점
        left = len(data[:num-1])
        right = len(data[num:])
        length = min(left, right)

        a = 0
        while a <= length :

            if data[num-1-a] == data[num-1+a] :
                if data[num-1-a] == 1:
                    data[num-1-a] = 0
                    data[num-1+a] = 0
                else:
                    data[num-1-a] = 1
                    data[num-1+a] = 1

            else:
                break
            a += 1

    
# print(data)
string = ''
for i in data:
    string += str(i)

aaa = ' '.join(string)

bb = 40
for j in range( (len(aaa)//20)+1 ):
    print(aaa[ bb*j : bb*(j+1)])
  • 지수 누나 풀이
n = int(input())
switch = list(map(int, input().split()))
people = int(input())
for _ in range(people):
    gender, number = map(int, input().split())
    # 성별 남자
    if gender == 1:
        for i in range(1, n+1):
            if not i%number: # i번째 전구가 주어진숫자(number)로 나눠지는 아이들만.
                if switch[i-1]: # i번째 전구의 인덱스
                    switch[i - 1] = 0
                else:
                    switch[i - 1] = 1
    # 성별 여자
    else:
        for i in range(0, n//2):
            if number-1+i not in range(n) or number-1-i not in range(n):
                break
            elif switch[number-1+i] != switch[number-1-i]:
                break
            elif switch[number-1+i] and switch[number-1-i]:
                switch[number - 1 + i] = 0
                switch[number - 1 - i] = 0
            elif not switch[number-1+i] and not switch[number-1-i]:
                switch[number - 1 + i] = 1
                switch[number - 1 - i] = 1
new_switch = [str(i) for i in switch]
for i in range(n//20+1):
    print(" ".join(new_switch[i*20:(i+1)*20]))
  • 쌉고수 풀이
import sys
input = lambda: sys.stdin.readline().rstrip()

n = int(input())
a = [0] + list(map(int, input().split()))
s_n = int(input())

switch = {0:1, 1:0}

for _ in range(s_n):
    g, num = map(int, input().split())
    if g == 1:
        for i in range(num, n+1, num):
            a[i] = switch[a[i]]
    else:
        a[num] = switch[a[num]]
        i = 1
        while (num - i >= 1) and (num + i <= n) and (a[num - i] == a[num + i]):
            a[num-i], a[num+i] = switch[a[num-i]], switch[a[num+i]]
            i+=1

for idx, el in enumerate(a[1:], start = 1):
    print(el, end=" ")
    if not idx % 20:
        print()

4. 색칠하기


# 4836. [파이썬 S/W 문제해결 기본] 2일차 - 색칠하기

def red(matri, r1, c1, r2, c2):
    for i in range(r1, r2+1):
        for j in range(c1, c2+1):
            if matri[i][j] == 0:
                matri[i][j] = 1
            elif matri[i][j] == 2:
                matri[i][j] = 3
    return matri

def blue(matri, r1, c1, r2, c2):
    for i in range(r1, r2+1):
        for j in range(c1, c2+1):
            if matri[i][j] == 0:
                matri[i][j] = 2
            elif matri[i][j] == 1:
                matri[i][j] = 3
    return matri



T = int(input())

for tc in range(1, T+1):
    res = 0
    # 10x10 2차원배열 생성
    matrix = [[0] * 10 for _ in range(10)]
    # pprint(matrix)

    # 칠할 영역 개수
    N = int(input())
    for _ in range(N):
        d = list(map(int, input().split()))
        c = d[4]
        # 빨간
        if c == 1:
            matrix = red(matrix, d[0], d[1], d[2], d[3])
        # 파랑
        else:
            matrix = blue(matrix, d[0], d[1], d[2], d[3])

    cnt = 0
    for i in range(10):
        for j in range(10):
            if matrix[i][j] == 3:
                cnt += 1
    res = cnt
    print('#{} {}'.format(tc, res))