📖 문제 링크
https://www.acmicpc.net/problem/10815
10815번: 숫자 카드
첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,
www.acmicpc.net
✅ 제출 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class Main { //최소 여행비용 구하기
static int N, M;
static List<Integer> card; //같은 숫자는 없음
static List<Integer> checkNum;
static boolean isNum;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = null;
N = Integer.parseInt(br.readLine());
card = new ArrayList<Integer>();
//상근이 카드
st = new StringTokenizer(br.readLine());
for(int i=0;i<N;i++) {
card.add(Integer.parseInt(st.nextToken()));
}
//확인해야할 숫자
M = Integer.parseInt(br.readLine());
checkNum = new ArrayList<Integer>();
st = new StringTokenizer(br.readLine());
for(int i=0;i<M;i++) {
checkNum.add(Integer.parseInt(st.nextToken()));
}
Collections.sort(card); //상근이가 갖고있는 카드는 정렬
for(int i=0;i<M;i++) {
isNum = false;
binarySearch(checkNum.get(i), 0, N);
if(isNum) bw.write(1 + " ");
else bw.write(0 + " ");
}
bw.flush();
bw.close();
}//main
public static void binarySearch(int num, int start, int end) {
if(start == end) return;
int mid = (start+end)/2;
if(num == card.get(mid)) {
isNum = true;
return;
}
if(num > card.get(mid)) {
binarySearch(num, mid+1, end);
}
else {
binarySearch(num, start, mid);
}
}//binarySearch
}//end class
'알고리즘 > Java' 카테고리의 다른 글
[백준/JAVA] 16935번 : 배열돌리기3 (0) | 2022.08.14 |
---|---|
[백준/JAVA] 16926번 : 배열 돌리기1 (0) | 2022.08.14 |
[백준/JAVA] 2751번 : 수정렬하기2 (0) | 2022.08.12 |
[SWEA/JAVA] 1861번 : 정사각형방 (0) | 2022.08.09 |
[SWEA/JAVA] 1208번 : Flatten (0) | 2022.08.02 |