Programming/Baekjoon

백준 1920 수찾기 [c++]

fishersheep 2022. 5. 30. 23:50
반응형
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>

using namespace std;

int n,m;
vector<int>v;
int t1, t2;

bool func(int a)
{

	int low = 0;
	int high = v.size() - 1;

	while (low <= high)	//low가 high와 같거나 작은동안 반복
	{	
		int mid = (low + high) / 2;	//현재값
		
		if (v[mid] == a)	//a를 찾은경우 true return
			return true;

		if (v[mid] < a)	//a보다 현재값이 작은경우
			low = mid+1;	//현재값+1을 low에 저장
		else if (v[mid] > a)
			high = mid-1;

	}

	return false;
}


int main()
{	
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> t1;
		v.push_back(t1);	//vector에 저장
	}

	sort(v.begin(), v.end());	//오름차순 정렬


	cin >> m;

	for (int i = 0; i < m; i++)
	{
		cin >> t2;	//찾아야할숫자

		if (func(t2))
			cout << "1" << '\n';	//true
		else
			cout << "0" << '\n';	//false
		
	}

	return 0;
}
반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 11286 절댓값 힙 [c++]  (0) 2022.05.31
백준 1012 유기농배추 [c++]  (0) 2022.05.31
백준 2980 도로와 신호등 [c++]  (0) 2022.05.21
백준 11279 c++ [최대힙]  (0) 2022.05.07
백준 1927 c++ [최소힙]  (0) 2022.05.07