https://school.programmers.co.kr/learn/courses/30/lessons/17681?language=java
레벨1
2진수를 만들 때는 해당 수를 나누어 나머지로 이용해서 만들면 된다.
9 -> 9를 2로 나누면 몫이 4이고 나머지가 1이다. 1이 맨 앞자리로 가고 몫을 다시 2로 나누어 그나머지가 다시 다음 자릿수가 된다. 이걸 반복하면 된다.
그런데 2진수의 경우 메소드가 있다.
그리고 참고로 비트연산자 | & 을 쓰면 진수끼리의 비교가 더욱 빠르다.
Integer.toBinaryString(num)
class Solution {
public String[] solution(int n, int[] arr1, int[] arr2) {
String[] answer = new String[n];
for( int i =0; i<n; i++){
int[] temp1 = to2(n, arr1[i]);
int[] temp2 = to2(n, arr2[i]);
StringBuilder sb = new StringBuilder();
for(int j=0; j<n; j++){
if(temp1[j]==0 && temp2[j]==0){
sb.append(" ");
}else{
sb.append("#");
}
}
answer[i] = sb.toString();
}
return answer;
}
public static int[] to2(int n, int num){
int[] temp = new int[n];
for(int i=0; i < n; i++){
temp[i] = num/(int)Math.pow(2,n-i-1);
num= num%(int)Math.pow(2,n-i-1);
}
return temp;
}
}
'Problem Solving > 구현' 카테고리의 다른 글
BOJ2744 대소문자 바꾸기 (0) | 2024.01.24 |
---|---|
자바 문법 - 진수 바꾸기 (3진법뒤집기) (0) | 2023.11.22 |
약수의 개수와 덧셈 (0) | 2023.11.13 |
과제진행하기 (0) | 2023.10.17 |
JadenCase 문자열 만들기 (0) | 2023.09.17 |