C# 1181번 단어 정렬

2022. 1. 10. 13:54C#/[백준] 정렬

문제

코드 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            int N = Convert.ToInt32(Console.ReadLine());
            List<string> words = new List<string>();

            for (int i = 0; i < N; i++)
            {
                words.Add(Console.ReadLine());
            }
            words = words.Distinct().ToList(); // 여기를 잘 기억하자
            var output = from word in words
                         orderby word.Length, word
                         select word;

            foreach (var item in output)
            {                
                sb.AppendLine(item);
            }
            Console.WriteLine(sb);

        }
    }
}

코드 풀이 :

Linq를 활용하면 굉장히 쉽게 풀 수 있는 문제이다.

'// 여기를 잘 기억하자' 는 개인적으로 Distinct를 사용할때 다시 리스트로 변환해서 저장해줘야한다는 것을 공부하기 위함이다.

 

깃허브 :

https://github.com/CheongHo-Lee/Algorithm-Study

 

GitHub - CheongHo-Lee/Algorithm-Study: Algorithm Study For Coding Test

Algorithm Study For Coding Test. Contribute to CheongHo-Lee/Algorithm-Study development by creating an account on GitHub.

github.com