728x90
2진법이란, 어떤 자연수를 0과 1로만 나타내는 것이다.
예를 들어 73은 64(2^6)+8(2^3)+1(2^0)이기 때문에 1001001으로 표현한다.
어떤 숫자를 입력받았을 때 그 숫자를 2진법으로 출력하는 프로그램을 작성하시오.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class Exam0406_1 {
public static void main(String[] args) {
for(int i=0;i<=100;i++) System.out.println(solution(i) + " : " + solution(i));
}
public static String solution(int a) {
return Integer.toBinaryString(a);
}
public static String solution2(int a) {
String result="";
while(a<2) {
result = a%2 + result;
a/=2;
}
return result;
}
}
|
cs |
728x90
'프로그래머스 코딩(자바) > Coding dojang' 카테고리의 다른 글
중간값 (0) | 2023.04.06 |
---|---|
palindrome (0) | 2023.04.06 |
완전수 구하기 (0) | 2023.04.05 |
10~1000까지 각 숫자 분해하여 곱하기의 전체 합 구하기 (0) | 2023.04.05 |
Special Pythagorean triplet (0) | 2023.04.05 |