정리용

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

알고리즘/백준

[백준 11723] 파이썬 - 집합

무룡룡 2022. 1. 11. 23:53

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

 

11723번: 집합

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

www.acmicpc.net

 

1. 코드설명

import sys
input = lambda : sys.stdin.readline().strip()

s=[]
for i in range(int(input())):
    a=input().split()

    if a[0] == 'all':
        s=[i for i in range(1,21)]
        continue
        
    elif a[0] == 'empty':
        s.clear()
        continue

    num=int(a[1])
    if a[0] == 'add' and num not in s:
        s.append(num)
        
    elif a[0] == 'remove' and num in s:
        s.remove(num)
        
    elif a[0] == 'check':
        print('1' if num in s else '0')
        
    elif a[0] == 'toggle':
        if num in s:
            s.remove(num)
        else:
            s.append(num)

 

 

2, 주의사항

lis = list(input().split())

숫자열(add... )과 문자열(1,2...) 를 한번에 받으려고 하면 all과 empty에서 오류발생 하므로

 

continue을 사용한다

 

 

Comments