콩부방
1416 : 2진수 변환 본문
728x90
어떤 10진수 n이 주어지면 2진수로 변환해서 출력하시오.
예)
10 -----> 1010
0 -----> 0
1 -----> 1
2 -----> 10
1024 -----> 10000000000
입력
10진수 정수 n이 입력된다.
(n은 21억이하의 임의의 수이다.)
출력
2진수로 변환해서 출력한다.
입력 예시 예시 복사
7
출력 예시
111
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(Integer.toBinaryString(n));
}//main end
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i=0,j=0;
int[] b = new int[500];
if(n == 0) System.out.println("0");//입력값이 0일경우
while(n>0){ // 2진수로 바꿔주는 부분
b[i] = n%2;
n /=2;
i++;
}
for (j = i-1; j >= 0; j--) {//출력
System.out.print(b[j]);
}
}//main end
}
728x90
'알고리즘 공부 > CodeUp' 카테고리의 다른 글
1425 : 자리 배치 (0) | 2021.08.31 |
---|---|
1420 : 3등 찾기 (0) | 2021.08.31 |
1412 : 알파벳 개수 출력하기 (0) | 2021.08.30 |
1411 : 빠진 카드 (0) | 2021.08.30 |
1410 : 올바른 괄호 1 (괄호 개수 세기) (0) | 2021.08.29 |
Comments