π» Coding Problems Solving/Hack
[JAVA] 2μ§μ, 8μ§μ, 16μ§μ + μ±μλ£κΈ°!
Kim_dev
2022. 10. 13. 03:42
μ«μ(int) -> μ§μ(String)
public class NumberConvert {
public static void main(String[] args) {
int decimal = 10;
String binary = Integer.toBinaryString(decimal); // 10μ§μ -> 2μ§μ
String octal = Integer.toOctalString(decimal); // 10μ§μ -> 8μ§μ
String hexaDecimal = Integer.toHexString(decimal); // 10μ§μ -> 16μ§μ
}
}
μ§μ(String) -> 10μ§μ(int)
2μ§μ -> 10μ§μ λ³ν : Integer.valueOf(string, 2);
8μ§μ -> 10μ§μ λ³ν : Integer.valueOf(string, 8);
16μ§μ -> 10μ§μ λ³ν : Integer.valueOf(string, 10);β
0μ±μλ£κΈ° ν¬λ§€ν
public class Main {
public static void main(String[] args) {
int intValue = 125;
String s = String.format("%05d", intValue);
System.out.println(s);
}
}
λΉλ°μ§λ (μ’μμμ)
class Solution {
public String[] solution(int n, int[] arr1, int[] arr2) {
String[] answer = new String[n];
for(int i=0;i<n;i++){
long temp1 = Long.parseLong(Long.toBinaryString(arr1[i]));
long temp2 = Long.parseLong(Long.toBinaryString(arr2[i]));
String bin1 = String.format("%0"+n+"d",temp1);
String bin2 = String.format("%0"+n+"d",temp2);
String temp = "";
for(int j=0;j<n;j++){
if(bin1.charAt(j) == '0' && bin2.charAt(j) == '0') temp += " ";
else temp += "#";
}
answer[i] = temp;
}
return answer;
}
}