문제 링크: https://www.acmicpc.net/problem/26069
from collections import deque
n = int(input())
li = deque()
for _ in range(n):
a, b = input().split()
if a == "ChongChong":
li.append(b)
elif b == "ChongChong":
li.append(a)
if a in li:
li.append(b)
elif b in li:
li.append(a)
print(len(set(li)))
처음 작성한 코드는 한 줄에 입력되는 두 이름들을 띄어쓰기를 기준으로 나누어서 a, b 변수에 입력받고,
a나 b에 "ChongChong" 이라는 이름이 들어오면 그 짝을 li라는 큐에 저장한다.
그리고나서 다음 줄에서 a나 b에 들어온 이름이 ChongChong과 함께 입력된적 있는 이름이라면
또한 li 큐에 저장한다.
마지막으로 set()으로 중복을 제거한 후 그 개수를 출력했다.
처음 작성한 코드로도 제출 했을 때 정답처리 되었지만 더 생각해보니 몇가지 문제점들이 있었다.
from collections import deque
n = int(input())
dance_people = {"ChongChong"}
queue = deque(["ChongChong"])
for _ in range(n):
a, b = input().split()
if a in dance_people or b in dance_people:
if a not in dance_people:
dance_people.add(a)
queue.append(a)
if b not in dance_people:
dance_people.add(b)
queue.append(b)
print(len(dance_people))
[Python/파이썬] 백준 # 10828 스택 (0) | 2025.04.08 |
---|---|
[Python/파이썬] 백준 #1181 단어 정렬 (0) | 2025.03.26 |
[Python/파이썬] 백준 #1157 단어 공부 (0) | 2025.03.22 |
[Python/파이썬] 백준 #1018 체스판 다시 칠하기 (0) | 2025.03.21 |
[Python/파이썬] 백준 #2346 풍선 터뜨리기 (0) | 2025.03.20 |