본문 바로가기

Study/algorithm

백준 자바 알고리즘 [8958 : OX퀴즈]

 

Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        String[] str = new String[num];

        for (int i = 0; i < num; i++) {
            int count = 0, sum = 0;
            str[i] = sc.next();
            for (int j =0; j < str[i].length(); j++) {
                if (str[i].charAt(j) == 'o')
                    sum += ++count;
                else count = 0;
            }
            System.out.println(sum);
        }

        sc.close();

 

string배열을 이용해서 해결.

str[0]에 'oooxxooxx' 이렇게 들어가 있다면

charAt()으로 각 인덱스의 값을 구할 수 있다.

그 인덱스 값이 o라면 count를 증가시켜서 값을 더하고 아니면 count값을 0으로 초기화 시켜준다.