동캄의 코딩도장

백준 11723 [집합] 파이썬 본문

코테/BOJ

백준 11723 [집합] 파이썬

동 캄 2023. 3. 20. 01:00

https://www.acmicpc.net/problem/11723

 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net

# 백준 11723 집합
import sys
input=sys.stdin.readline
M=int(input())
S=[]
for _ in range(M):
    cmd=list(map(str,input().split())) 
    if len(cmd)==2:
        command,num=cmd[0],cmd[1] #명령 + 숫자 인 경우
        num=int(num)
    else:
        command=cmd[0] #명령만 있는 경우
    if command=='add': 
        if not S.count(num):
            S.append(num)
    elif command=='remove':
        if S.count(num):
            S.remove(num)
    elif command=='check':
        if S.count(num):
            print(1)
        else:
            print(0)
    elif command=='toggle':
        if S.count(num):
            S.remove(num)
        else:
            S.append(num)
    elif command=='all':
        S=[i for i in range(1,21)]
    elif command=='empty':
        S=[]

명령어를 받아서, 각 경우에 대해 분류하고 명령대로 구현되도록 코드를 만든다.