입력받는 X의 값을 2진수로 표현하여 1의 개수 카운터해줬다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main1094_{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int X = Integer.parseInt(br.readLine());
String str = Integer.toBinaryString(X);
int count = 0;
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i) =='1') {
count++;
}
}
System.out.println(count);
}//end of main
}//end of class
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs |
'Algorithm 문제풀이 > simulation' 카테고리의 다른 글
[BOJ] 백준 14890 - 경사로(JAVA) (0) | 2019.07.24 |
---|---|
[BOJ] 백준 3190 - 뱀(JAVA) (0) | 2019.07.23 |
[BOJ] 백준 14503 - 로봇청소기(JAVA) (0) | 2019.07.16 |
[BOJ] 백준 3055 - 탈출 (JAVA) (1) | 2019.07.11 |
[BOJ] 백준 1966 - 프린터큐 (JAVA) (0) | 2019.06.29 |