코딩 테스트/BOJ

C# 1181 단어 정렬

s0002 2023. 1. 12. 23:01
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Study11
{
    class App
    {
        //생성자 
        public App()
        {
            string[] arr = {
                "but", "i", "wont", "hesitate", "no", "more", "no", "it", "cannot", "wait", "im", "your"
            };

            //여기부터 작성하세요 
            string[] distinctArr = arr.Distinct().ToArray(); //먼저 중복 제거한 배열을 만들었다

            IOrderedEnumerable<string> query = from word in distinctArr
                           orderby word.Length, word //길이가 짧은 것 먼저, 같으면 사전순으로 나오게 했다
                           select word;

            foreach(string result in query)
            {
                Console.WriteLine(result);
            }

            //출력 
            //i
            //im
            //it
            //no
            //but
            //more
            //wait
            //wont
            //yours
            //cannot
            //hesitate

        }
    }
}

 

참고

https://developer-talk.tistory.com/215

 

[C#]배열 중복 값 제거(Distinct)

배열에서 중복 값을 제거한다는 의미는 고유한 값만 가진다는 의미입니다. 하지만, C#에서는 배열의 값을 제거할 수 없기 때문에 중복 값이 없는 새로운 배열을 생성해야 합니다. 이러한 경우 Dis

developer-talk.tistory.com

https://learn.microsoft.com/ko-kr/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations

 

기본 LINQ 쿼리 작업(C#)

LINQ 쿼리 식 및 쿼리에서 수행할 수 있는 작업 중 일부를 소개합니다.

learn.microsoft.com

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

 

1181번: 단어 정렬

첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.

www.acmicpc.net

'코딩 테스트 > BOJ' 카테고리의 다른 글

C# 10988 팰린드롬  (0) 2023.01.17
C# 1120 문자열  (0) 2023.01.13
C# 10173 니모를 찾아서  (0) 2023.01.12
C# 4458 첫 글자를 대문자로  (0) 2023.01.12
C# 2711 오타맨 고창영  (0) 2023.01.12