C# 1181번 단어 정렬
2022. 1. 10. 13:54ㆍ코딩테스트 문제 풀이/[백준] 정렬
728x90
반응형
코드 :
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
728x90
반응형
'코딩테스트 문제 풀이 > [백준] 정렬' 카테고리의 다른 글
C# 18870번 좌표 압축 (0) | 2022.01.10 |
---|---|
C# 10814번 나이순 정렬 (0) | 2022.01.10 |
C# 11650번 좌표 정렬하기, 11651번 좌표 정렬하기2, Linq문법 (0) | 2022.01.10 |
C# 1427번 소트인사이드 (0) | 2022.01.10 |
C# 2750번 수 정렬하기, 2751번 수 정렬하기2 (0) | 2022.01.10 |