반응형
문제를 읽어 보았을때 정수가 존재하는지 알아내는지 찾아본다는 구문에 이전에 공부했던 HashSet을 사용해서 풀었는데 알고리즘 분류에는 이분 탐색으로 되어있었다.
HashSet을 이용해서 풀면 금방풀린다. 다음에 이분 탐색으로 다시 풀어봐야겠다.
HashSet 알아보러가기
C# - Set과 HashSet(Add,Remove,Contains,IntersectWith,UnionWith)
Set(Abstract data type) 01 중복을 허용하지 않는다. 중복된 값을 허용하지 않으므로 데이터의 유일성을 보장한다. 그러므로 같은 값을 여러번 저장하더라도 실제로 한 번만 저장된다. 02 순서를 보장
code-piggy.tistory.com
코드 전문
using System;
using System.Collections.Generic;
using System.Text;
namespace baek2
{
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
string[] a_string = Console.ReadLine().Split();
HashSet<double> A = new HashSet<double>();
for(int i = 0; i< a_string.Length;i++)
{
A.Add(double.Parse(a_string[i]));
}
int m = int.Parse(Console.ReadLine());
string[] b_string = Console.ReadLine().Split();
StringBuilder sb = new StringBuilder();
for(int i = 0; i<b_string.Length;i++)
{
if (A.Contains(double.Parse(b_string[i]))) sb.AppendLine("1");
else sb.AppendLine("0");
}
Console.WriteLine(sb.ToString());
}
}
}
반응형
'코딩테스트 준비 > 백준 C#' 카테고리의 다른 글
백준 C# - 1620 +) 풀이 (0) | 2024.02.24 |
---|---|
백준 C# - 7785 (1) | 2024.02.24 |
백준 C# - 15666 +) 풀이 (0) | 2024.02.21 |
백준 C# - 15664 +) 풀이 (0) | 2024.02.21 |
백준 C# - 15663 +) 풀이 (0) | 2024.02.21 |
댓글