첫 줄에 명단의 크기 n, 둘째 줄에 정수 n개(명단), 셋째 줄에 질문 수 q, 넷째 줄에 정수 q개가 주어진다. 질문한 수마다 명단에 그 수가 있으면 "예", 없으면 "아니요"를 한 줄에 하나씩 출력하라. 명단에는 같은 수가 여러 번 나올 수 있다.
입력
1 ≤ n ≤ 1,000, 1 ≤ q ≤ 1,000, 모든 수는 -1,000,000,000 이상 1,000,000,000 이하의 정수
출력
q줄, 줄마다 "예" 또는 "아니요"
예시 1 · stdin
3
10 20 300
4
20 1 300 30
stdout
예
아니요
예
아니요
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
목록에 있는지 묻는 도구: Python x in a, JavaScript·TypeScript a.includes(x), C# a.Contains(x)(배열이면 using System.Linq), C++ std::find(a.begin(), a.end(), x) != a.end()(C++23은 std::ranges::contains), Java 배열은 Arrays.stream(a).anyMatch(v -> v == x), C는 반복문 함수 하나. 크레이콘은 a.있나(x)예요. 함정 셋: ① 숫자를 글자로 찾으면 조각도 걸려요. 줄 "10 20 300"에서 "1"·"30"을 찾으면 있다고 나와요. ② Java Arrays.asList(a).contains(x)는 a가 int[]이면 List<int[]>(배열 하나짜리 목록)가 되어 늘 false예요. 컴파일 오류도 나지 않아요. ③ 반복문 안에 else return 0을 두면 첫 칸만 보고 "없다"고 끝내요. "없다"는 다 돌아 본 뒤에야 말할 수 있어요. 이 도구들은 모두 앞에서부터 하나씩 보므로 질문마다 n번까지 봐요. 질문이 아주 많으면 Python set, Java HashSet 같은 집합을 써요.
Pythonmain.py · 5 줄
n = int(input())a = list(map(int, input().split()))q = int(input())for x in map(int, input().split()): print("예" if x in a else "아니요")
import java.util.Arrays;import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); int q = sc.nextInt(); StringBuilder out = new StringBuilder(); for (int k = 0; k < q; k++) { int x = sc.nextInt(); boolean found = Arrays.stream(a).anyMatch(v -> v == x); out.append(found ? "예" : "아니요").append('\n'); } System.out.print(out); }}
C#Main.cs · 17 줄
using System;using System.Linq;class Program{ static void Main() { Console.ReadLine(); int[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); Console.ReadLine(); foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { int x = int.Parse(s); Console.WriteLine(a.Contains(x) ? "예" : "아니요"); } }}
C++main.cpp · 16 줄
#include <algorithm>#include <iostream>#include <vector>int main() { int n, q, x; std::cin >> n; std::vector<int> a(n); for (int& v : a) std::cin >> v; std::cin >> q; for (int k = 0; k < q; k++) { std::cin >> x; bool found = std::find(a.begin(), a.end(), x) != a.end(); std::cout << (found ? "예" : "아니요") << "\n"; }}
Cmain.c · 19 줄
#include <stdio.h>int contains(const int *a, int n, int x) { for (int i = 0; i < n; i++) if (a[i] == x) return 1; return 0;}int main(void) { int n, q, x, a[1000]; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &q); for (int k = 0; k < q; k++) { scanf("%d", &x); puts(contains(a, n, x) ? "예" : "아니요"); } return 0;}
Craykonmain.cray · 7 줄
n = 콘솔.줄읽기().수로변환();a = 콘솔.수들읽기();q = 콘솔.줄읽기().수로변환();각각 (x 에서 콘솔.수들읽기()){ 콘솔.쓰기(a.있나(x) ? "예" : "아니요");}