[백준] 9375, 10828 - Python3

shsh·2021년 12월 8일
0

백준

목록 보기
31/45

10828. 스택

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

내 풀이 - 성공

from sys import stdin

N = int(stdin.readline())

stack = []

for _ in range(N):
    cmd = stdin.readline().split()
    
    if cmd[0] == "push":
        stack.append(int(cmd[1]))
    elif cmd[0] == "pop":
        if stack:
            print(stack.pop())
        else:
            print(-1)
    elif cmd[0] == "size":
        print(len(stack))
    elif cmd[0] == "empty":
        if stack:
            print(0)
        else:
            print(1)
    elif cmd[0] == "top":
        if stack:
            print(stack[-1])
        else:
            print(-1)

stack 을 하나 만들고

문제 고대로 조건에 따라 구분

push : append
pop : 가능하다면 pop, 아니면 -1
size : stack 의 크기
empty : 스택이 비어있는지 아닌지 여부
top : 스택의 가장 마지막 값 또는 -1


9375. 패션왕 신해빈

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

내 풀이 - 성공

from sys import stdin
import collections

N = int(stdin.readline())

for _ in range(N):
    n = int(stdin.readline())
    clothes = collections.defaultdict(int)
    ans = 1
    for _ in range(n):
        cloth = stdin.readline().split()
        clothes[cloth[1]] += 1
    for k, v in clothes.items():
        ans *= v+1
    print(ans-1)

모든 의상 종류마다 착용하지 않는 경우도 추가해서 계산

의상의 이름은 상관이 없으므로 (같은 이름의 의상이 입력으로 들어오지도 않음)
의상의 개수만 세서 clothes 에 저장

ans 는 1 로 초기화하고
의상 개수 + 1 (착용 X 의 경우) 을 모두 곱해준 후
알몸일 경우 한가지를 빼주고 print

profile
Hello, World!

0개의 댓글