코딩 공부/C#
[C# / 백준] 1181번 문제 - 단어 정렬
통통푸린
2024. 1. 22. 12:01
728x90
반응형
문제
알파벳 소문자로 이루어진 N개의 단어가 들어오면 아래와 같은 조건에 따라 정렬하는 프로그램을 작성하시오.
- 길이가 짧은 것부터
- 길이가 같으면 사전 순으로
단, 중복된 단어는 하나만 남기고 제거해야 한다.
입력
첫째 줄에 단어의 개수 N이 주어진다. (1 ≤ N ≤ 20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
출력
조건에 따라 정렬하여 단어들을 출력한다.
예제 입&출력
소스코드
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
int cnt = int.Parse(Console.ReadLine());
List<string> list = new List<string>();
for(int i = 0; i<cnt; i++)
{
string buff = Console.ReadLine();
list.Add(buff);
}
var sortlist = list.OrderBy(x => x.Length).ThenBy(x => x).Distinct();
//첫번째 길이, 두번째 알파벳순, 세번째 중복삭제
foreach(string s in sortlist)
{
sb.AppendLine(s);
}
Console.WriteLine(sb);
}
}
}
728x90
반응형