๐Ÿ‘ฉ‍๐Ÿ’ปProgramming/Coding Test

[C#] [BOJ#8958] OXํ€ด์ฆˆ - ToCharArray

taesooya 2022. 7. 30. 23:42

https://www.acmicpc.net/problem/8958

 

2741๋ฒˆ: N ์ฐ๊ธฐ

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, 1๋ถ€ํ„ฐ N๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

www.acmicpc.net

My Solution

๋‚˜๋Š” ๊ณต๋ฐฑ ์—†๋Š” ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด๋กœ ๋งŒ๋“œ๋‚˜ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ ์ด๋Ÿด ๋• ToCharArray


using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int t = int.Parse(Console.ReadLine());
            for (int i = 0; i < t; i++)
            {
                int sum = 0;
                int count = 0;

                string s = Console.ReadLine();
                ~~char[] chars = s.ToCharArray();~~

                for (int j = 0; j < chars.Length; j++)
                {
                    if (chars[j] != 'X')
                    {
                        count++;
                        sum += count;
                    }
                    else
                    {
                        count = 0;
                    }
                }
                Console.WriteLine(sum);
            }
        }
    }
}

Solution


using System;
 
class Program {
 
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        string[] testCase = new string[n];
 
        for (int i = 0; i < n; i++)
        {
            testCase[i] = Console.ReadLine();
            char[] temp = testCase[i].ToCharArray();
            int j = 0;
            int resultScore = 0;
            int score = 0;
 
            while (true)
            {
                if(temp[j].ToString() == "O")
                {
                    score++;
                    resultScore += score;
                }
                else
                {
                    score = 0;
                }
 
                j++;
 
                if (j == temp.Length) break;
            }
 
            Console.WriteLine(resultScore);
        }
    }
}