File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
main/java/io/github/imsejin/study/baekjoon/bronze
test/groovy/io/github/imsejin/study/baekjoon/bronze Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change
1
+ package io .github .imsejin .study .baekjoon .bronze ;
2
+
3
+ import java .io .BufferedReader ;
4
+ import java .io .InputStreamReader ;
5
+ import java .math .BigInteger ;
6
+
7
+ public class P1373 {
8
+
9
+ public static void main (String [] args ) throws Exception {
10
+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (System .in ))) {
11
+ String binary = reader .readLine ();
12
+
13
+ String answer = solve1 (binary );
14
+ System .out .println (answer );
15
+ }
16
+ }
17
+
18
+ static String solve1 (String binary ) {
19
+ if (binary .equals ("0" )) return "0" ;
20
+
21
+ int length = binary .length ();
22
+ StringBuilder sb = new StringBuilder ();
23
+
24
+ int bit = 0 ;
25
+ for (int i = 1 ; i <= length ; i ++) {
26
+ char c = binary .charAt (length - i );
27
+
28
+ int r = i % 3 ;
29
+ if (c == '1' ) {
30
+ int b = switch (r ) {
31
+ case 1 -> 1 ;
32
+ case 2 -> 2 ;
33
+ case 0 -> 4 ;
34
+ default -> throw new IllegalArgumentException ();
35
+ };
36
+
37
+ bit += b ;
38
+ }
39
+
40
+ if (r == 0 ) {
41
+ sb .append (bit );
42
+ bit = 0 ;
43
+ }
44
+ }
45
+
46
+ if (bit > 0 ) sb .append (bit );
47
+
48
+ return sb .reverse ().toString ();
49
+ }
50
+
51
+ static String solve2 (String binary ) {
52
+ return new BigInteger (binary , 2 ).toString (8 );
53
+ }
54
+
55
+ }
Original file line number Diff line number Diff line change
1
+ package io.github.imsejin.study.baekjoon.bronze
2
+
3
+ import spock.lang.Specification
4
+
5
+ class P1373Spec extends Specification {
6
+
7
+ def " test" () {
8
+ when :
9
+ def actual = P1373 . solve1(binary)
10
+
11
+ then :
12
+ actual == octal
13
+ actual == P1373 . solve2(binary)
14
+
15
+ where :
16
+ binary | octal
17
+ " 0" | " 0"
18
+ " 111" | " 7"
19
+ " 1000" | " 10"
20
+ " 1010" | " 12"
21
+ " 11001100" | " 314"
22
+ }
23
+
24
+ }
You can’t perform that action at this time.
0 commit comments