CodingTest/Baekjoon

[baekjoon] 백준 10816번(파이썬): 숫자 카드 2

JunJangE 2021. 9. 20. 15:15

문제

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

알고리즘

- collections 모듈에 Counter 클래스를 사용하여 문제를 수행한다.

- 반복문을 통해 구해야 할 정수가 있는지 확인한다.

- 구해야 할 정수가 있다면 정수의 개수를 구해야 할 카드 자리에 넣어준다.

코드

import sys
from collections import Counter # collections 모듈에 Counter 클래스 사용

n = int(sys.stdin.readline())
n_card = list(map(int, sys.stdin.readline().split()))
m = int(sys.stdin.readline())
m_card = list(map(int, sys.stdin.readline().split()))

# 숫자 카드에 적혀있는 정수의 개수를 세어준다.
cnt = Counter(n_card)

# 결과 리스트
res = [0] * m

# 구해야 할 정수가 있는지 반복문을 통해 확인한다.
for i in range(m):

    # 구해야 할 정수가 있다면 정수의 개수를 구해야 할 카드 자리에 넣어준다.
    if cnt[m_card[i]]:
        res[i] = cnt[m_card[i]]

print(*res)

github

 

GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법

내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.

github.com