Baekjun Number 10815 Card (with.Java)
This article explores Baekjoon Problem 10815, Number Card (with Java).
We will learn by solving coding test problems, reflecting on past problems, and exploring different solution methods.
Letβs first look at the problem.
Problem
A number card is a card with a single integer written on it.
Sanggeun has N number cards.
Given M integers, write a program that determines whether Sanggeun has a number card with this number written on it.
Input
The first line contains the number N of number cards Sanggeun has (1 β€ N β€ 500,000).
The second line contains the integers written on the number cards.
The numbers written on the number cards are greater than or equal to -10,000,000 and less than or equal to 10,000,000.
No two number cards have the same number written on them.
The third line contains M (1 β€ M β€ 500,000).
The fourth line contains M integers, separated by spaces, for which the number cards must be checked.
This number is greater than or equal to -10,000,000 and less than or equal to 10,000,000.
Output
For the M numbers given in the first line of input, print 1 if Sanggeun has a number card with each number written on it, or 0 if not, separated by a space.
Problem Solution
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.InputStreamWriter;
import java.util.HashSet;
class Main{
public static void main(String[] args) throws IOException {
HashSet<String> set = new HashSet<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
br.readLine();
String[] temp = br.readLine().split(" ");
br.readLine();
String[] card = br.readLine().split(" ");
String[] answer = new String[card.length];
br.close();
for(int i = 0; i < temp.length; i++){
set.add(temp[i]);
}
for(int i =0; i < card.length; i++){
if(set.contains(card[i])){
answer[i] = "1";
}else{
answer[i] = "0";
}
}
for(String str : answer){
bw.write(str + " ");
}
bw.flush();
bw.close();
}
}
Solution Explanation
This function compares two string arrays to determine if they intersect, and outputs the result.
First, during input processing, it uses a BufferedReader to read the two arrays, compares their elements, and outputs the result.
For efficient set operations, it uses a HashSet to efficiently handle input and output.
This allows you to quickly check for intersection between two arrays and output the result.
Thank you!