1159번: 농구 경기 (acmicpc.net)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace _230120_1
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();            

            int a = int.Parse(sr.ReadLine());

            List<string> nameList = new List<string>();

            for (int i = 0; i < a; i++)
            {
                nameList.Add(sr.ReadLine());
            }

            nameList.Sort(); // 이름 목록을 리스트에 저장하고 오름차순 정렬
            int cnt = 1;

            for(int i = 0; i < nameList.Count - 1; i++) 
            {
                if(nameList[i][0] == nameList[i+ 1][0]) //이름의 첫글자가 같으면 수를 cnt++;
                {
                    cnt++;
                }
                else
                {
                    cnt = 1;
                }

                if(cnt == 5)
                {
                    sb.Append(nameList[i][0]);  //같은 글자가 5개가 되면 해당 문자를 스트링빌더에 추가
                }
            }

            if (sb.Length > 0)
            {
                for(int i = 0; i < sb.Length - 1; i++)
                {
                    if(sb[i] == sb[i + 1])  //같은 문자열이 중복되었을 시 제거
                    {
                        sb.Remove(i, sb[i]);
                    }
                }
                sw.Write(sb);
            }
            else
            {
                sw.WriteLine("PREDAJA");
            }            

            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

입력받은 이름의 첫글자를 아스키코드로 변환하여 정수형 배열에 추가하는 방식으로 풀 수 있지만 과정을 좀 더 단순화하여 해결하였습니다.

'Algorithm > BOJ' 카테고리의 다른 글

C#) [BOJ] 1302 베스트 셀러  (0) 2023.01.22
C#) [BOJ] 1940 주몽  (0) 2023.01.20
C#) [BOJ] 1251 단어나누기 ★  (0) 2023.01.20
C#) [BOJ] 10808 알파벳 개수  (0) 2023.01.18
C#) [BOJ] 1439 뒤집기  (0) 2023.01.17

+ Recent posts