Notice
Recent Posts
Recent Comments
-
[백준 10816] 숫자 카드 2 - 파이썬 본문
반응형
문제
https://www.acmicpc.net/problem/10816
풀이
n = int(input())
cards = list(map(int, input().split()))
m = int(input())
numbers = list(map(int, input().split()))
count = {}
for i in cards:
if i in count:
count[i] += 1
else:
count[i] = 1
for i in numbers:
result = count.get(i)
if(result == None):
print(0, end=' ')
else:
print(result, end=' ')
딕셔너리를 활용한 풀이다.
1. 카드의 종류와 상근이의 숫자 카드를 cards, numbers 리스트에 각각 넣는다.
2. cards 에 있는 카드들을 확인하며 딕셔너리에서 해당 카드의 value 값을 1 올려준다.
3. numbers 에 있는 카드들을 확인하며 딕셔너리에서 해당 카드의 value 값을 출력한다.
처음에 시간초과 난 정답
n = int(input())
cards = list(map(int, input().split()))
m = int(input())
numbers = list(map(int, input().split()))
count = []
for i in numbers:
count.append(cards.count(i))
print(*count)
반응형
'Algorithm' 카테고리의 다른 글
에라토스테네스의 체 (소수 구하기 알고리즘) (0) | 2023.04.29 |
---|---|
[백준 1929] 소수 구하기 - 파이썬 (0) | 2023.04.29 |
[백준 4949] 균형잡힌 세상 - 파이썬 (0) | 2023.04.26 |
[백준 2164] 카드 2 - 파이썬 (0) | 2023.04.25 |
[백준 1920] 수 찾기 - 파이썬 (0) | 2023.04.24 |
Comments