작성일 :

문제 링크

11536번 - 줄 세우기

설명

여러 사람의 이름이 주어졌을 때,

해당 이름들의 순서가 사전순으로 오름차순인지, 내림차순인지, 아니면 둘 다 아닌지를 판별하는 문제입니다.


접근법

  • 먼저 이름의 개수를 입력받고, 한 줄씩 이름들을 차례대로 입력받습니다.
  • 이후 이름 리스트의 정렬 전후 상태를 비교하여,
    • 원래 순서가 정렬된 것과 같으면 "INCREASING"을 출력하고,
    • 원래 순서가 역순 정렬된 것과 같으면 "DECREASING"을 출력합니다.
    • 둘 다 아니라면 "NEITHER"를 출력합니다.

Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Linq;

class Program {
  static void Main() {
    int n = int.Parse(Console.ReadLine());
    var names = new string[n];
    for (int i = 0; i < n; i++)
      names[i] = Console.ReadLine();

    var asc = names.OrderBy(x => x).ToArray();
    var desc = names.OrderByDescending(x => x).ToArray();

    if (names.SequenceEqual(asc)) Console.WriteLine("INCREASING");
    else if (names.SequenceEqual(desc)) Console.WriteLine("DECREASING");
    else Console.WriteLine("NEITHER");
  }
}

C++

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

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

  int n; cin >> n;
  string prv; cin >> prv;
  int cnt = 1;
  for (int i = 1; i < n; i++) {
    string cur; cin >> cur;
    cnt += cur < prv ? -1 : 1;
    prv = cur;
  }

  if (cnt == n) cout << "INCREASING\n";
  else if (cnt == 2 - n) cout << "DECREASING\n";
  else cout << "NEITHER\n";

  return 0;
}