-
백준 2810(JAVA) - 컵홀더Algorithm 2022. 11. 17. 09:38728x90
오늘 풀어볼 문제는 백준 2810 컵홀더 문제이다.
문제에서 원하는 정보는 굉장히 간단하다. S면 카운팅해주고, L이면 한 칸 넘기고 카운팅해달라는 뜻이다.
단, SSS인 경우와 같이 모든 좌석보다 카운팅 수가 커지는 경우에는 좌석 갯수를 그대로 출력시키면 된다.
package selftest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main_2810 { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); String str = br.readLine(); int count = 1; for(int i = 0; i<N; i++) { if(str.charAt(i) == 'S') { count++; } else if(str.charAt(i) == 'L') { i++; count++; } } if(N > count) { System.out.println(count); } else { System.out.println(N); } } }
원하는 정보를 좌석수 : N, 컵홀더 count : count로 String 형으로 입력받은 str을 charAt()을 사용하여 그대로 출력시켜주면 간단하다. 단, SSS와 같은 경우의 수를 체크하기 위하여 N < count의 경우를 N(좌석 수)를 그대로 출력시키는 형태로 해결할 수 있다.
728x90'Algorithm' 카테고리의 다른 글
백준 15953(JAVA) - 상금헌터 (0) 2022.11.24 백준 2751(JAVA) - 수 정렬하기2 (0) 2022.11.22 백준 2512(JAVA) - 예산 (0) 2022.11.15 백준 10814(JAVA) - 나이순 정렬 (0) 2022.11.14 백준 4673(JAVA) - 셀프 넘버 (0) 2022.11.14