[백준] 11723번 집합

거북이·2023년 1월 3일
0

백준[실버5]

목록 보기
30/114
post-thumbnail

💡문제접근

명령의 길이를 0과 1에 따라서 구분한 다음 어떤 명령을 수행할 것인지 분류하여 코드를 작성했다.

💡코드(메모리 : 30616KB, 시간 : 6160ms)

import sys

M = int(input())
data_set = set()
for _ in range(M):
    input_command = list(map(str, sys.stdin.readline().strip().split()))
    if len(input_command) == 2:
        command, data = input_command[0], int(input_command[1])
        if command == "add":
            if data in data_set:
                continue
            else:
                data_set.add(data)
        elif command == "remove":
            if data in data_set:
                data_set.remove(data)
            else:
                continue
        elif command == "check":
            if data in data_set:
                print(1)
            else:
                print(0)
        elif command == "toggle":
            if data in data_set:
                data_set.remove(data)
            else:
                data_set.add(data)
    else:
        command = input_command[0]
        if command == "all":
            data_set = set([i for i in range(1, 21)])
        elif command == "empty":
            data_set.clear()

💡소요시간 : 10m

0개의 댓글