2024-03-22 00:09:58

첫번째 줄에 띄어쓰기 없이 숫자가 주어진다.

주어진 수를 오름차순 정렬이 아닌, 내림차순 정렬을 해서 띄어쓰기 없이 한 줄로 출력하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
            StringBuilder sb = new StringBuilder();
 
            string str = reader.ReadLine();
            int[] Dec = new int[str.Length];
 
 
            for(int i = 0; i < str.Length; i++)
            {
                Dec[i] = str[i] - '0';
            }
 
            for(int j=0; j<Dec.Length; j++)
            {
                int temp = 0;
                for(int k=j; k<Dec.Length; k++)
                {
                    if (Dec[j] < Dec[k])
                    {
                        temp = Dec[k];
                        Dec[k] = Dec[j];
                        Dec[j] = temp;
                    }
                }
            }
            foreach (int a in Dec)
                sb.Append(a);
 
            print.Write(sb);
 
        }
    }
}
cs

이번 문제는 내림차순을 구현하는 문제이다.

비록 Array.sort(Dec); Array.Reverse(Dec); 혹은

Dec = Dec.OrderByDesCending(x => x).ToArray(); 메소드를 사용해서 구현할 수 있겠지만,

이번 문제의 핵심은 지금까지는 오름차순을 사용했겠지만 이번엔 "직접 내림차순을 구현해 봐라"

라는 의미로 와닿아서 직접 구현을 해봤다.