[BOJ] 1920 수 찾기
Algorithm 2023. 1. 25. 02:39https://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
이분탐색
using System;
using System.IO;
namespace Assignment01
{
class Program
{
private static int[] arr;
static StreamReader sr = new StreamReader(Console.OpenStandardInput());
static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
static void Main(string[] args)
{
int n = int.Parse(sr.ReadLine());
arr = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
Array.Sort(arr);
int m = int.Parse(sr.ReadLine());
var arr2 = Array.ConvertAll(sr.ReadLine().Split(), int.Parse);
sr.Close();
for (int i = 0; i < m; i++)
bsearch(arr2[i]);
sw.Close();
}
static void bsearch(int n)
{
int start = 0;
int end = arr.Length-1;
int mid = 0;
while (start <= end)
{
mid = (start + end) / 2;
if (arr[mid] == n)
{
sw.WriteLine("1");
return;
}else if (arr[mid] > n)
{
end = mid - 1;
}
else
{
start = mid + 1;
}
}
sw.WriteLine("0");
}
}
}
'Algorithm' 카테고리의 다른 글
[BOJ] 10989 수 정렬하기 3 (0) | 2023.01.26 |
---|---|
[BOJ] 1654 랜선 자르기 (0) | 2023.01.25 |
[BOJ] 1343 폴리오미노 (0) | 2023.01.24 |
C#으로 구현한 우선순위 큐 Priority Queue (0) | 2023.01.24 |
[BOJ] 2684 동전 게임 (0) | 2023.01.21 |