본문 바로가기
코딩테스트 준비/백준 C#

백준 C# - 1620 +) 풀이

by 코딩하는 돼징 2024. 2. 24.
반응형

이 문제를 풀기 앞서 할 수 있어야 하는 2가지

01 Dictionary에서 value이용해서 Key값 가져오는 법

Dictionary<string, string> dic = new Dictionary<string, string>();
var keys = new List<string>(dic.Keys);
string key = keys[4];

02 Dictionary에서 Key값 이용해서 Value값 가져오는 법

string keyToFind = "piggy";
string value = dict[keyToFind];

코드 전문

using System;
using System.Collections.Generic;
using System.Text;
namespace baek2
{
    class Program
    {
        public static void Main()
        {
            string[] token = Console.ReadLine().Split();
            int n = int.Parse(token[0]);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            StringBuilder sb = new StringBuilder();
            
            for(int i = 1; i<=n;i++)
            {
                string name = Console.ReadLine();
                dic.Add(name, i.ToString());
            }
            var keys = new List<string>(dic.Keys);
            for (int i = 0; i<int.Parse(token[1]);i++)
            {
                string s = Console.ReadLine();
                if (char.IsDigit(s[0]))
                {
                    string key = keys[int.Parse(s)-1];
                    sb.AppendLine(key);
                }
                else
                {
                    sb.AppendLine(dic[s]);
                }
            }
            Console.WriteLine(sb.ToString());
        }
    }
}
반응형

'코딩테스트 준비 > 백준 C#' 카테고리의 다른 글

백준 C# - 11725 +) 풀이  (0) 2024.03.04
백준 C# - 17219  (1) 2024.02.24
백준 C# - 7785  (1) 2024.02.24
백준 C# - 1920 +) 풀이  (0) 2024.02.24
백준 C# - 15666 +) 풀이  (0) 2024.02.21

댓글