|
| 1 | +https://www.acmicpc.net/problem/10815 |
| 2 | + |
| 3 | +자기가 가지고 있는 숫자가 있을때 상대방이 제시하는 숫자가 있는지 확인하는 문제였다. |
| 4 | + |
| 5 | +두개의 풀이로 해결하였다. |
| 6 | + |
| 7 | +하나의 풀이는 map을 이용해 즉 hash를 이용해 상대방이 제시하는 숫자가 있는지 확인하는 방법 |
| 8 | + |
| 9 | +두번쨰는 이분탐색으로 값이 존재하는지 확인하는 방법 이었다. |
| 10 | + |
| 11 | +``` |
| 12 | +import java.util.*; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +
|
| 15 | +public class Main { |
| 16 | + public static void main(String[] args) { |
| 17 | + Scanner sc = new Scanner(System.in); |
| 18 | + int N = Integer.parseInt(sc.nextLine()); // 숫자카드 N개 주어짐 |
| 19 | + Map<String, String> nums = Arrays.stream(sc.nextLine().split(" ")) |
| 20 | + .collect(Collectors.toMap( |
| 21 | + key -> key, |
| 22 | + value -> value, |
| 23 | + (a,b) -> a |
| 24 | + )); |
| 25 | + List<String> result = new ArrayList<>(); |
| 26 | + int M = Integer.parseInt(sc.nextLine()); // 정수 M개가 주어졌을때 적혀있는 숫자를 가지고 있는지 아닌지 |
| 27 | + String[] mstr = sc.nextLine().split(" "); |
| 28 | + for (int i = 0; i < mstr.length; i++) { |
| 29 | + if(nums.containsKey(mstr[i])) { |
| 30 | + result.add("1"); |
| 31 | + } else { |
| 32 | + result.add("0"); |
| 33 | + } |
| 34 | + } |
| 35 | + System.out.println(String.join(" ", result)); |
| 36 | +
|
| 37 | + } |
| 38 | +} |
| 39 | +
|
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +``` |
| 44 | +package binary_search; |
| 45 | +
|
| 46 | +import java.util.*; |
| 47 | +import java.util.stream.Collectors; |
| 48 | +
|
| 49 | +public class b10815 { |
| 50 | + public static void main(String[] args) { |
| 51 | + Scanner sc = new Scanner(System.in); |
| 52 | + int N = Integer.parseInt(sc.nextLine()); // 숫자카드 N개 주어짐 |
| 53 | + Integer[] str = Arrays.stream(sc.nextLine().split(" ")) |
| 54 | + .map(Integer::parseInt) |
| 55 | + .toArray(Integer[]::new); |
| 56 | + Arrays.sort(str); |
| 57 | +
|
| 58 | +
|
| 59 | + List<String> result = new ArrayList<>(); |
| 60 | + int M = Integer.parseInt(sc.nextLine()); // 정수 M개가 주어졌을때 적혀있는 숫자를 가지고 있는지 아닌지 |
| 61 | + String[] mstr = sc.nextLine().split(" "); |
| 62 | + for (int i = 0; i < mstr.length; i++) { |
| 63 | + if(hasNum(str, Integer.parseInt(mstr[i]))) { |
| 64 | + result.add("1"); |
| 65 | + } else { |
| 66 | + result.add("0"); |
| 67 | + } |
| 68 | + } |
| 69 | + System.out.println(String.join(" ", result)); |
| 70 | +
|
| 71 | + } |
| 72 | +
|
| 73 | + public static boolean hasNum(Integer[] str, Integer num) { |
| 74 | + int start = 0; |
| 75 | + int end = str.length - 1; |
| 76 | +
|
| 77 | + while(start <= end) { |
| 78 | + int mid = start + end / 2; |
| 79 | +
|
| 80 | + if(Objects.equals(str[mid], num)) { |
| 81 | + return true; |
| 82 | + } else if(str[mid] > num) { |
| 83 | + end = mid - 1; |
| 84 | + } else { |
| 85 | + start = mid + 1; |
| 86 | + } |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | +} |
| 91 | +
|
| 92 | +``` |
0 commit comments