Programming/BaekJoon

백준 8958번 OX퀴즈

helloworld: 2021. 10. 14. 23:03

 

 


내가 푼 풀이

배열을 이용하되 테스트케이스에 대한 배열을 만들었으면 

이 풀이보다 필요한 변수도 줄어들고 코드도 짧아지는데

배열에 점수를 넣는 것으로 접근해서 이렇게 코드가 길~어졌다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		//테스트 케이스의 개수
		int N = Integer.parseInt(br.readLine());
		
		//점수를 계산하기 위한 변수 
		int score = 0;
		int score2 = 0;
        
        	//테스트케이스별 점수를 보관할 int 배열
		int[] score3 = new int[N];
        
        	//테스트 케이스 입력받기
		for(int i = 0; i < N; i++) {
			String s = br.readLine();
			
		//테스트케이스를 반복문 돌며 연속된 O의 개수만큼 점수 플러스
			for(int j = 0; j < s.length(); j++) {
				if(s.charAt(j) == 'O') {
					score++;
					score2 += score;
				} else {
					score = 0;
				}
			}
            //점수 넣기
			score3[i] = score2;
            
            //변수 초기화(다음 테스트케이스의 점수 계산을 위해)
			score2 = 0;
			score = 0;
		}
		
        	//점수 출력
		for(int i : score3) {
			System.out.println(i);
		}
	}

}