Algorithm/BOJ

[BOJ] 1181 단어 정렬

Game Client Lee Hwanguk 2023. 1. 17. 22:50
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp8
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //[BOJ] 1181 단어 정렬

            string[] arr = {
                "but", "i", "wont", "hesitate", "no", "more", "no", "it", "cannot", "wait", "im", "your"
            };

            //여기부터 작성하세요 

            //Linq를 이용해서 정리해보자
            //

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

            //1.단어의 길이를 보고 정렬
            //2.길이가 같다면 사전순으로 정렬
            //3.같은 단어는 두번 출력되지않는다

            //정렬-링큐를 복습하고 돌아오자
            //검색을 좀 해보니 .Sort라는 기능이있다 사전순으로 정리해준다한다 검증해보자
            //sort는 List의 요소를 정렬한다한다 리스트에 먼저 담자

            //List<string> list = new List<string>();
            //for(int i=0; i<arr.Length; i++)
            //{
            //    list.Add(arr[i]);
            //}


            //list.Sort(); //정렬해보자

            //foreach (var list0 in list)
            //{
            //    Console.WriteLine(list0);
            //}
            ////---------------------------------------------------
            
            


        }
    }
}

#1. 문제를 읽고 혼자 쭉 풀어보았다 

#1)첫번째 문제는 단어 길이 순으로 오름차순으로 정리해야겠다 ->오름차순?Linq를 써야겠다 책을 뒤져보자

#2)같은 단어는 두번 출력하지 않는다(중복제거) ->이런기능이있는 메서드가 분명히 있을거같다

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

namespace ConsoleApp8
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //[BOJ] 1181 단어 정렬

            string[] arr = {
                "but", "i", "wont", "hesitate", "no", "more", "no", "it", "cannot", "wait", "im", "your"
            };

            //여기부터 작성하세요 

            //Linq를 이용해서 정리해보자
            //

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

            //1.단어의 길이를 보고 정렬
            //2.길이가 같다면 사전순으로 정렬
            //3.같은 단어는 두번 출력되지않는다

            //정렬-링큐를 복습하고 돌아오자
            //검색을 좀 해보니 .Sort라는 기능이있다 사전순으로 정리해준다한다 검증해보자
            //sort는 List의 요소를 정렬한다한다 리스트에 먼저 담자

            //List<string> list = new List<string>();
            //for(int i=0; i<arr.Length; i++)
            //{
            //    list.Add(arr[i]);
            //}


            //list.Sort(); //정렬해보자

            //foreach (var list0 in list)
            //{
            //    Console.WriteLine(list0);
            //}
            ////---------------------------------------------------
            //김동준님 블로그 참고
            //Linq를 사용하셨다

            IEnumerable<string> sequence = from ar in arr //from -> arr을 ar에 넣으면서 순회
                                           orderby ar ascending     //orderby->오름차순으로 정리(사전순으로 정리가된다)
                                           orderby ar.Length ascending  //orderby->글자 수 대로 오름차순으로 정렬 (같은orderby를 두번을쓸수있다, 필터를 두개나 쓸수있다)                              
                                           select ar;  //출력? 바로 ar을 담아서 출력가능할까?

            sequence.Distinct();       //Distinct(); 메서드로 중복된 요소들을 제거한다
            IEnumerable<string> sequence2 = sequence.Distinct(); //IEnumerable<string> sequence2 에 다시 담아준다
            foreach (string ar in sequence2)                           //컬렉션 요소 출력
            {

                Console.WriteLine(ar); //오름차순으로 정리하고, 글자 수 대로 오름차순으로 정리하고, 중복된요소를 제거한 sequence2를 ar에 담아서 출력.
            }

            

        }
    }
}

#김동준님 블로그를 참고했다 Linq만 쓰려했는데 .Disetinct(); 메서드를 쓰셨다 

#기능이 기가막히다. 중복된 요소를 자동으로 지워줬다

 

#깔끔하게 해결되었다