일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- skt fellowship 3기
- 졸프
- yolo
- pandas
- 2021 제9회 문화공공데이터 활용경진대회
- javascript
- google 로그인
- 코드업
- AWS
- 양방향 매핑
- OG tag
- matplotlib
- 커스텀 데이터 학습
- Spring Boot
- C++
- 순환참조
- idToken
- Spring
- react native
- YOLOv5
- Loss Function
- JPA
- google login
- @Transactional
- STT
- Expo
- oauth
- marksense.ai
- html
- google cloud
- Today
- Total
민팽로그
[백준/BOJ] 1920: 수 찾기 본문
https://www.acmicpc.net/problem/1920
1920번: 수 찾기
첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들
www.acmicpc.net
문제
N개의 정수 A[1], A[2], …, A[N]이 주어져 있을 때, 이 안에 X라는 정수가 존재하는지 알아내는 프로그램을 작성하시오.
입력
첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들이 A안에 존재하는지 알아내면 된다. 모든 정수의 범위는 -231 보다 크거나 같고 231보다 작다.
출력
M개의 줄에 답을 출력한다. 존재하면 1을, 존재하지 않으면 0을 출력한다.
예제 입력
5
4 1 5 2 3
5
1 3 7 9 5
예제 출력
1
1
0
0
1
코드
1) algorithm 헤더파일의 binary_search 사용
#include <iostream>
#include<algorithm>
using namespace std;
int arr_n[100001], arr_m[100001];
int main() {
int n, m;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr_n[i];
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> arr_m[i];
}
bool res;
sort(arr_n, arr_n + n);
for (int i = 0; i < m; i++) {
res = binary_search(arr_n, arr_n + n, arr_m[i]);
if (res) cout <<"1\n";
else cout << "0\n";
}
return 0;
}
2) 이분 탐색 함수 직접 구현 - 1
#include <iostream>
#include<algorithm>
using namespace std;
int arr_n[100001];
bool binarySearch(int start, int end, int item) {
if (start > end) return false;
int mid = (start + end) / 2;
if (arr_n[mid] == item) return true;
else {
if (arr_n[mid] > item) return binarySearch(start, mid - 1, item);
else return binarySearch(mid + 1, end, item);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr_n[i];
}
cin >> m;
int item;
bool res;
sort(arr_n, arr_n + n);
for (int i = 0; i < m; i++) {
cin >> item;
res = binarySearch(0, n - 1, item);
if (res) cout <<"1\n";
else cout << "0\n";
}
return 0;
}
3) 이분 탐색 함수 직접 구현 - 2
#include <iostream>
#include<algorithm>
using namespace std;
int arr_n[100001];
bool binarySearch(int start, int end, int item) {
while (start <= end) {
int mid = (start + end) / 2;
if (arr_n[mid] == item) return true;
if (arr_n[mid] > item) end = mid - 1;
else start = mid + 1;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr_n[i];
}
cin >> m;
int item;
bool res;
sort(arr_n, arr_n + n);
for (int i = 0; i < m; i++) {
cin >> item;
res = binarySearch(0, n - 1, item);
if (res) cout << "1\n";
else cout << "0\n";
}
return 0;
}
C++ algorithm 헤더파일에서 binary_search 함수를 제공하지만 직접 짜봐야 할 것 같아서 구현해보았다. 알고리즘 수업시간에 배웠는데 오랜만에 다시 짜보았다.
처음에는 함수의 재귀 호출을 통해 구현했고, 검색을 해보니 while문으로도 구현하길래 두가지 방식 모두 작성해보았다.
'PS > 백준📖' 카테고리의 다른 글
[백준 / BOJ] 2805: 나무 자르기 (0) | 2022.04.04 |
---|---|
[백준 / BOJ] 1654: 랜선 자르기 (0) | 2022.03.31 |
[백준/BOJ] 7579: 앱 (0) | 2022.03.13 |
[백준 / BOJ] 2293: 동전 1 (0) | 2022.03.03 |
[백준/BOJ] 2629: 양팔저울 (0) | 2022.03.02 |