Algorithm/BOJ

[BOJ] 10808 알파벳 개수 C# 풀이

Bueong_E 2023. 1. 19. 01:06
반응형
SMALL

문제풀이

using System;
using System.Collections.Generic;
using System.Linq;

namespace BOJ00
{
    class Program
    {
        static void Main()
        {          
            List<int> tempList1 = new List<int>();
            List<char> alphabats= new List<char>();
            List<char> tempLists2 = new List<char>();

            string words = Console.ReadLine();

            for (int i =97; i <123; i++)            //  알파벳의 아스키코드들을 정수리스트에 담기            
                tempList1.Add(i);                
            
            foreach (int ascii in tempList1) // 정수리스트에 담긴 아스키코드들을 알파벳으로 변환후 알파벳 리스트에 담기        
                alphabats.Add((char)ascii);
                                          
            for (int j = 0;j < alphabats.Count; j++) // 알파벳 리스트를 순회하며 요소를 탐색
            {                                  
                    for(int f =0; f < 26; f++)          // 입력된 문자의 문자열을 돌며 알파벳과 비교
                    {
                        if (f == words.Length) break; // 입력된 문자의 인덱스 끝에 다다르면 break;

                        else if (alphabats[j] == words[f])  //아니라면 선택된 알파벳과 비교후 해당 알파벳이 맞다면 임시 리스트2에 담기
                        tempLists2.Add(alphabats[f]);                       
                    }
                    Console.Write("{0} ",tempLists2.Count);      // 임시 리스트에 담긴 요소수를 반환
                    tempLists2.Clear();                          // 반환이 끝나면 리스트2 비우고 다음 알파벳과 비교시작
            }
        }
    }    
}

결과창

부르트포스로 풀은 문제인데char 문자 형식 변환에 대해 조금더 유연하게 이해할수 있게 된....듯 하다

다른방법은 없으려나..

반응형
LIST