문제 링크: https://www.acmicpc.net/problem/10816
백준 집합과 맵 4단계 - 10816번 숫자 카드 2를 풀어보았다.
풀이: 카드의 개수를 세면 된다.
C++의 경우, upper_bound와 lower_bound를 썼다. 변수로 줄 땐 auto형을 써야 한다. 원래는 count함수를 썼는데, 시간초과가 났다.
파이썬의 경우, 카운터를 썼다.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m,a;
cin>>n;
vector<int>nv;
vector<int>mv;
for(int i=0;i<n;i++)
{
cin>>a;
nv.push_back(a);
}
cin>>m;
sort(nv.begin(),nv.end());
for(int i=0;i<m;i++)
{
cin>>a;
auto up=upper_bound(nv.begin(),nv.end(),a);
auto dn=lower_bound(nv.begin(),nv.end(),a);
mv.push_back(up-dn);
}
for(auto b:mv)
{
cout<<b<<" ";
}
}
Python
from collections import Counter
n=int(input())
card=list(map(int,input().split()))
m=int(input())
num=list(map(int,input().split()))
cnt=Counter(card)
for i in num:
if i in cnt: print(cnt[i],end=' ')
else:print(0,end=' ')
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
대칭 차집합 풀이 (0) | 2023.01.09 |
---|---|
듣보잡 풀이 (0) | 2023.01.06 |
나는야 포켓몬 마스터 이다솜 풀이 (0) | 2023.01.06 |
문자열 집합 풀이 (0) | 2023.01.06 |
숫자 카드 풀이 (0) | 2023.01.06 |