jeongwon
[백준 JAVA] 8958번 - OX 퀴즈 본문

나의 답안:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main{
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String [] arr = new String[Integer.parseInt(bf.readLine())];
for(int i=0; i<arr.length; i++)
arr[i]=bf.readLine();
int score=1;
int[] tot=new int[arr.length];
for(int i=0; i<arr.length; i++) {
score=1;
for(int j=0; j<arr[i].length(); j++) {
if(88==arr[i].charAt(j) || arr[i].charAt(j)==120)
score=1;
else if(79==arr[i].charAt(j) || arr[i].charAt(j)==111) {
tot[i]+=score;
score++;
}
}
}
for(int i : tot)
System.out.println(i);
bf.close();
}
}
개선 답안 :
package back;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main{
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String [] arr = new String[Integer.parseInt(bf.readLine())];
for(int i=0; i<arr.length; i++) {
arr[i]=bf.readLine();
}
for(int i=0; i<arr.length; i++) {
int score=0;
int tot=0;
for(int j=0; j<arr[i].length(); j++) {
if(arr[i].charAt(j)=='O')
score++;
else {
score=0;
}
tot+=score;
}
System.out.println(tot);
}
bf.close();
}
}
for문 내에서 위치 선정만 잘 했다면 반복문 횟수는 물론, 배열 생성 횟수도 줄일 수 있는 간단한 문제였다.
개선 답안 참고 : https://st-lab.tistory.com/50
'오늘의 문제' 카테고리의 다른 글
| [백준 JAVA] 1065번 - 한수 (0) | 2022.06.01 |
|---|---|
| [백준 JAVA] 4344번 - 평균은 넘겠지 (0) | 2022.05.30 |
| [백준 JAVA] 1546번 - 평균 (0) | 2022.05.27 |
| [백준 JAVA] 3052번 - 나머지 (0) | 2022.05.27 |
| [백준 JAVA] 2577번 - 숫자의 개수 (0) | 2022.05.26 |