작성일 :

문제 링크

15688번 - 수 정렬하기 5

설명

N개의 정수를 비내림차순으로 정렬하여 출력하는 문제입니다. 같은 수가 여러 번 중복될 수 있습니다.


접근법

모든 수를 배열에 저장한 뒤 기본 정렬 함수로 정렬합니다. 입력 크기가 최대 100만 개이므로 빠른 입출력을 사용해야 시간 제한을 통과할 수 있습니다.



Code

C#

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
using System;
using System.Text;

class FastScanner {
  readonly byte[] buf = new byte[1 << 16];
  int idx, len;
  int ReadByte() {
    if (idx >= len) {
      len = Console.OpenStandardInput().Read(buf, 0, buf.Length);
      idx = 0;
      if (len == 0) return -1;
    }
    return buf[idx++];
  }
  public int NextInt() {
    int c, sign = 1, val = 0;
    do c = ReadByte(); while (c <= ' ');
    if (c == '-') { sign = -1; c = ReadByte(); }
    while (c > ' ') { val = val * 10 + (c - '0'); c = ReadByte(); }
    return val * sign;
  }
}

class Program {
  static void Main() {
    var fs = new FastScanner();
    var n = fs.NextInt();
    var arr = new int[n];
    for (var i = 0; i < n; i++) arr[i] = fs.NextInt();

    Array.Sort(arr);

    var sb = new StringBuilder();
    for (var i = 0; i < n; i++) sb.AppendLine(arr[i].ToString());
    Console.Write(sb.ToString());
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vi v(n);
  for (int i = 0; i < n; i++) cin >> v[i];

  sort(v.begin(), v.end());

  for (int x : v) cout << x << "\n";

  return 0;
}