Baekjoon algorithm training

[백준] 8958 (OX퀴즈)

interfacer_han 2023. 11. 27. 15:10

#1 알고리즘

O 또는 X 어느 쪽에서든 점수 갱신을 한다고 생각한다. 점수 갱신 전에, O는 더할 점수를 1만큼 증가시키고, X는 더할 점수를 0으로 만든다.

 

#2 코드

#2-1 자바

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int testCaseCount = Integer.parseInt(sc.nextLine().trim());
        String submit = "";
        
        for(int i = 0; i < testCaseCount; i++) {
            char[] testCase = sc.nextLine().trim().toCharArray();
            
            int totalScore = 0;
            int upcomingScore = 0;
            
            for(char c : testCase) {
                if(Character.valueOf(c).equals('O')) {
                    upcomingScore++;
                    
                } else { // Character.valueOf(c).equals('X')
                    upcomingScore = 0;
                }
                
                totalScore += upcomingScore;
            }
            
            submit += Integer.toString(totalScore) + "\n";
        }
        
        sc.close();
        
        System.out.println(submit);
    }
}

 

#2-2 코틀린

fun main() {
    val testCaseCount = readln()!!.trim().toInt()
    var submit : String = ""
    
    for(i : Int in 1..testCaseCount) {
        val testCase = readln()!!.trim()
        
        var totalScore = 0
        var upcomingScore = 0
        
        // O 또는 X로 이루어진 문자열 순회
        for(char in testCase) {
            if(char == 'O') {
                upcomingScore++
                
            } else { // char == "X"
                upcomingScore = 0
            }
            
            totalScore += upcomingScore
        }
        
        submit += "${totalScore}\n"
    }
    
    println(submit)
}

'Baekjoon algorithm training' 카테고리의 다른 글

[백준] 4153 (직각삼각형)  (0) 2023.11.30
[백준] 11651 (좌표 정렬하기 2)  (0) 2023.11.28
[백준] 10828 (스택)  (0) 2023.11.25
[백준] 10818 (최소, 최대)  (0) 2023.11.24
[백준] 10250 (ACM 호텔)  (0) 2023.11.23