작성일 :

문제 링크

6751번 - From 1987 to 2013

설명

주어진 연도 다음부터 시작해 자릿수가 모두 다른 해를 찾는 문제입니다.


접근법

연도를 1씩 늘리며 네 자릿수가 모두 다른지 확인합니다.
조건을 만족하는 첫 해를 출력합니다.


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

class Program {
  static bool Distinct(int y) {
    var seen = new bool[10];
    while (y > 0) {
      var d = y % 10;
      if (seen[d]) return false;
      seen[d] = true;
      y /= 10;
    }
    return true;
  }

  static void Main() {
    var y = int.Parse(Console.ReadLine()!);
    while (true) {
      y++;
      if (Distinct(y)) {
        Console.WriteLine(y);
        return;
      }
    }
  }
}

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
#include <bits/stdc++.h>
using namespace std;

bool distinctYear(int y) {
  bool seen[10] = {};
  while (y > 0) {
    int d = y % 10;
    if (seen[d]) return false;
    seen[d] = true;
    y /= 10;
  }
  return true;
}

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

  int y; cin >> y;
  while (true) {
    y++;
    if (distinctYear(y)) {
      cout << y << "\n";
      return 0;
    }
  }
}