[JAVA] Boj_12927 | 배수 스위치
2021, Feb 26
문제
- [Boj 12927- 배수 스위치]
- 해결방법 : 배열의 index를 활용해서 for문을 잘 사용한다면 어려운 문제가 아니다 쉽게 접근하자
나의 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package swea; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Arrays; | |
public class Boj_12927 { | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
char[] swi = br.readLine().toCharArray(); | |
final int SIZE = swi.length; | |
int cnt=0; | |
for(int i=0;i<SIZE;i++) { | |
if(swi[i]=='Y') { | |
cnt++; // 전구가 다 꺼질때까지 반복문을 돌릴꺼므로 총 개수를 센다 | |
} | |
} | |
int answer=0; | |
//전구가 다 꺼질 때까지 | |
while(cnt>0) { | |
//반복해서 더해줄 (배수) 시작 위치를 찾는다 | |
int repeat=0; | |
for(int i=0;i<SIZE;i++) { | |
if(swi[i]=='Y') { | |
repeat=i+1; //0부터 시작하므로 +1해주는게 중요 | |
break; | |
} | |
} | |
// 시작 위치만 -1 해줘야함 | |
// 위에서 +1 | |
for(int i=repeat-1;i<SIZE;i+=repeat) { | |
if(swi[i]=='Y') { | |
swi[i]='N'; | |
cnt--; | |
}else { | |
swi[i]='Y'; | |
cnt++; | |
} | |
} | |
answer++; | |
// System.out.println(Arrays.toString(swi)); | |
} | |
System.out.println(answer); | |
} | |
} |