Algorithm/CodeUp
[CodeUp_Java] Q1416 : 말하는 앵무새
Plitche
2022. 7. 26. 13:43
반응형
안녕하세요! Plitche(플리체)입니다.
이번 포스팅의 주제는 Q1416 2진수 변환 (자바, JAVA)입니다.
Intro
Question
문제 설명
입력
10진수 정수 n이 입력된다.
(n은 21억이하의 임의의 수이다.)
출력
2진수로 변환해서 출력한다.
예시
- 입력 : 7
- 출력 : 111
Solution (풀이)
- 풀이 1 : 메모리 12484, 시간 97
public class Answer1 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
int tens = sc.nextInt();
int two=1;
int times=1;
while (tens/two>1) {
two*=2;
times+=1;
}
for (int i=0; i<times; i++) {
if (tens>=two) {
sb.append(1);
tens-=two;
} else sb.append(0);
two /= 2;
}
System.out.println(sb);
sc.close();
}
}
- 풀이 2 : 메모리 11116, 시간 67
public class Answer2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int tens = Integer.parseInt(br.readLine());
int two=1;
int times=1;
while (tens/two>1) {
two*=2;
times+=1;
}
for (int i=0; i<times; i++) {
if (tens>=two) {
sb.append(1);
tens-=two;
} else sb.append(0);
two /= 2;
}
System.out.println(sb);
br.close();
}
}
Ranking(순위)
반응형