하루 한 접시

[백준] 1620 나는야 포켓몬 마스터 이다솜 [C#]

NaZZU 2024. 3. 26. 23:33

 

1번부터 N번까지 N마리의 포켓몬의 이름이 주어지고, 그중 M마리의 포켓몬을 맞춰야 한다.

맞출 포켓몬은 이름으로 주어지기도 하고, 번호로도 주어지기도 한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();
 
            int[] NM = Array.ConvertAll(reader.ReadLine().Split(), int.Parse);
            int N = NM[0]; int M = NM[1];
 
            Dictionary<stringint> pokemon = new Dictionary<stringint>();
            Dictionary<intstring> idx = new Dictionary<intstring>();
 
            for (int i = 1; i < N+1; i++)
            {
                string mon = reader.ReadLine();
                pokemon.Add(mon, i);
                idx.Add(i, mon);
            }
 
 
            for (int j = 0; j < M; j++)
            {
                string str = reader.ReadLine();
                if (pokemon.ContainsKey(str))
                    sb.Append(pokemon[str] + "\n");
                else if (idx.ContainsKey(int.Parse(str)))
                    sb.Append(idx[int.Parse(str)] + "\n");
            }
 
            print.WriteLine(sb);
 
        }
    }
}
cs

 

너무 마음에 안드는 코드다. 키값을 이용해 밸류값을 알아내는 방법은 알겠는데, 역순으로는 어떻게 해야 할지 모르겠다.

좀 더 고민해보고, 코드를 다시 짜봐야겠다.