Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

jeongwon

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

오늘의 문제

[백준 JAVA] 8958번 - OX 퀴즈

jeongwon_ 2022. 5. 28. 17:34

 

나의 답안: 

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