쏘ing

[Python] 백준 11723 집합 본문

알고리즘/CLASS 3

[Python] 백준 11723 집합

한민민 2022. 2. 8. 15:27

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

풀이 1_리스트

from collections import deque
import sys

num = int(sys.stdin.readline())

num_list = deque()

for _ in range (num):
    word = sys.stdin.readline().rstrip().split()
    
    if len(word) == 2:
        x = int(word[1])
        
    if word[0] == 'add':
        if x in num_list:
            pass
        else:
            num_list.append(x)
    elif word[0] == 'remove':
        if x in num_list:
            num_list.remove(x)
        else:
            pass
    elif word[0] == 'check':
        if x in num_list:
            print(1)
        else:
            print(0)
    elif word[0] == 'toggle':
        if x in num_list:
            num_list.remove(x)
        else:
            num_list.append(x)
    elif word[0] == 'all':
        num_list = [i+1 for i in range(20)]
    elif word[0] == 'empty':
        num_list = []

풀이 2_집합

import sys

num = int(sys.stdin.readline())

num_list = set()

for _ in range (num):
    word = sys.stdin.readline().rstrip().split()
    
    if len(word) == 1:
        if word[0] == 'all':
            num_list = set([i for i in range(1, 21)])
        else:
            num_list = set()
    else:
        x = int(word[1])
        word = word[0]
        
        if word == 'add':
            num_list.add(x)
        elif word == 'remove':
            num_list.discard(x)
        elif word == 'check':
            if x in num_list:
                print(1)
            else:
                print(0)
        elif word == 'toggle':
            if x in num_list:
                num_list.discard(x)
            else:
                num_list.add(x)
Comments