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으로 초기화 시켜준다.
'Study > algorithm' 카테고리의 다른 글
백준 자바 알고리즘 [4344 : 평균은 넘겠지] (0) | 2020.05.03 |
---|---|
백준 자바 알고리즘 [나머지] (0) | 2020.05.02 |
백준 자바 알고리즘 [숫자의 개수] (0) | 2020.04.30 |
백준 자바 알고리즘 [최댓값] (0) | 2020.04.30 |
백준 자바 알고리즘 [최소,최대] (0) | 2020.04.30 |