-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.java
More file actions
53 lines (44 loc) ยท 1.78 KB
/
Main.java
File metadata and controls
53 lines (44 loc) ยท 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package seunguk.thisiscodingtest.part03.Q03_reverse;
import java.util.Scanner;
// 11101101 ์ด ์์ ๋ ์ซ์๊ฐ ๋ฐ๋ ๋ (1 -> 0, 0 -> 1) ๋ ๊ฐ์ง ๊ฒฝ์ฐ์ค
// ํ์๊ฐ ๋ ์์ ํ์ ์ถ๋ ฅ
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String exampleString = sc.nextLine();
char[] exampleChars = exampleString.toCharArray();
int oneToZero = 0; // 1์ด์๋ค๊ฐ 0์ผ๋ก ๋ฐ๋ ์นด์ดํธ
int zeroToOne = 0; // 0์ด์๋ค๊ฐ 1๋ก ๋ฐ๋ ์นด์ดํธ
boolean check = false;
// 1 -> 0 ๋ฐ๋๋ ์นด์ดํธ ์ธ์ค๋ค.
for (char exampleChar : exampleChars) {
if (check) { // check๊ฐ true์ด๋ฉด 1์์ 0์ผ๋ก ๋ฐ๋์๋ค๋ ๊ฒ
if (exampleChar == '0') { // true์ผ ๋ 0์ด๋ฉด ์ฐ์๋๋ 0์ด๋ฏ๋ก ์นด์ดํธ๋ฅผ ์ธ์ง์๊ณ continue
continue;
} else { // 1์ธ๊ฒฝ์ฐ๋ ๋ค์ check๋ฅผ false๋ก ๋ฐ๊ฟ์ค๋ค.
check = false;
}
}
if (exampleChar == '0') { // check๊ฐ false ์ด๋ฏ๋ก ํ์ฌ 0์ธ ๊ฒฝ์ฐ๋ 1 -> 0 ์ผ๋ก ๋ฐ๋ ์ํ์ด๋ฏ๋ก ์นด์ดํธ ์ฆ๊ฐ
oneToZero++;
check = true;
}
}
check = false; // check ์ด๊ธฐํ
// 0 -> 1 ๋ฐ๋๋ ์นด์ดํธ ์ธ์ค๋ค.
for (char exampleChar : exampleChars) {
if (check) {
if (exampleChar == '1') {
continue;
} else {
check = false;
}
}
if (exampleChar == '1') {
zeroToOne++;
check = true;
}
}
System.out.println(Math.min(oneToZero, zeroToOne));
}
}