SMALL

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

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net


  • 문제


  • 문제풀이

A부터 Z까지 순서대로 나열해서 max를 카운팅해준다.


  • 코드 1
using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string word = Console.ReadLine().ToUpper();
            int[] count = new int[26];
            
            foreach (char c in word)
            {
                count[c - 'A']++;
            }
            
            int maxcnt = 0;
            char maxChar = 'A';
            bool two = false;
            
            for (int i = 0; i < 26; i++)
            {
                if (count[i] > maxcnt)
                {
                    maxcnt = count[i];
                    maxChar = (char)('A' + i);
                    two = false;
                }
                else if (count[i] == maxcnt)
                {
                    two = true;
                }
            }
            
            Console.WriteLine(two ? "?" : maxChar.ToString());
        }
    }
}

  • 후기

문자열 탐색때도 비슷한방법을 썼던것 같다.

LIST

+ Recent posts