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

[C#] [BOJ#1157] ๋‹จ์–ด ๊ณต๋ถ€

taesooya 2022. 7. 30. 23:42

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

 

1157๋ฒˆ: ๋‹จ์–ด ๊ณต๋ถ€

์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

 

My Solution

  • ํŠน์ • ๋ฌธ์ž ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ (๋žŒ๋‹ค์‹) _ ๋ฐ‘์ค„ 1
  • Linq๋ฅผ ์ด์šฉํ•œ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์˜ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ_๋ฐ‘์ค„ 2

using System;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine().ToUpper();
            string alphab = "ABCDEFGHIJKLMNOPQRSTUVWZYX";
            char[] alphabets = alphab.ToCharArray();

            int[] count = new int[26];

            for (int i = 0; i < 26; i++)
            {
                count[i] = s.Count(f => f == alphabets[i]);
            }
            int max = count.Max();
            int position = Array.IndexOf(count, max);
            Array.Sort(count);
            Array.Reverse(count);
            if (count[0] == count[1])
            {
                Console.WriteLine("?");
            }
            else Console.WriteLine(alphabets[position]);
        }
    }
}