Notice
Recent Posts
Recent Comments
05-20 20:45
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

-

[백준 10816] 숫자 카드 2 - 파이썬 본문

Algorithm

[백준 10816] 숫자 카드 2 - 파이썬

choiht 2023. 4. 27. 14:05
반응형

문제

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

 

10816번: 숫자 카드 2

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

www.acmicpc.net

 

 

 

풀이

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)
반응형
Comments