๐Algorithm/๐ฅBaekjoon
[Baekjoon] 12927_๋ฐฐ์ ์ค์์น
๋ฟ์ผ._.
2024. 4. 11. 23:20
๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/12927)
< ๋ฐฐ์ ์ค์์น >
๋ฌธ์ ํ์ด
์์์๋ถํฐ ์ ๊ตฌ๋ฅผ ๋๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _12927_ { // ๋ฐฐ์ ์ค์์น
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
boolean[] arr = new boolean[str.length() + 1];
for (int i = 1; i <= str.length(); i++) {
if (str.charAt(i - 1) == 'Y') {
arr[i] = true;
}
}
int result = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i]) {
result += 1;
int idx = i;
while (idx < arr.length) {
arr[idx] = !arr[idx];
idx += i;
}
}
}
System.out.println(result);
}
}
๋ณ์)
str : ์ ๊ตฌ ์ํ
arr : ์ ๊ตฌ ์ํ boolean ๊ฐ (true: ์ผ์ง, false: ๊บผ์ง)
result : ๋ชจ๋ ์ ๊ตฌ๋ฅผ ๋๊ธฐ ์ํด ๋๋ฌ์ผ ํ๋ ์ค์์น ์
์ ๊ตฌ์ ์ํ๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์ ๊ตฌ์ ์ํ๋ฅผ ๋ณด๋ฉฐ ์ ๊ตฌ๊ฐ ์ผ์ ธ ์๋ค๋ฉด ๋ฐฐ์ด ๊ฐ์ true, ๊บผ์ ธ์๋ค๋ฉด false๋ก ์ ์ฅํ๋ค. ๋ฐฐ์ด์ ์์์๋ถํฐ ํ์ํ๋ฉด์ ๊ฐ์ด true์ด๋ฉด result+1์ ํ ํ ๋ฐฐ์์ ๊ฐ์ ๋ฐ๋ ๊ฐ์ผ๋ก ๋ฐ๊ฟ์ค๋ค.
์ต์ข ๋ชจ๋ ์ ๊ตฌ๋ฅผ ๋๊ธฐ ์ํด์ ๋๋ฌ์ผ ํ๋ ์ค์์น ์ result๋ฅผ ์ถ๋ ฅํ๋ค.