작성일 :

문제 링크

1681번 - 줄 세우기

설명

숫자 L이 포함되지 않는 양의 정수를 작은 것부터 N개 고를 때, 그중 가장 큰 수를 출력하는 문제입니다.


접근법

1부터 차례대로 확인하며 숫자 L이 포함되지 않는 수만 카운트합니다. 카운트가 N이 되는 순간의 수가 답입니다.


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

class Program {
  static bool HasDigit(int x, int d) {
    while (x > 0) {
      if (x % 10 == d) return true;
      x /= 10;
    }
    return false;
  }

  static void Main() {
    var parts = Console.ReadLine()!.Split();
    var n = int.Parse(parts[0]);
    var l = int.Parse(parts[1]);

    var count = 0;
    var cur = 1;
    while (true) {
      if (!HasDigit(cur, l)) {
        count++;
        if (count == n) {
          Console.WriteLine(cur);
          return;
        }
      }
      cur++;
    }
  }
}

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

bool hasDigit(int x, int d) {
  while (x > 0) {
    if (x % 10 == d) return true;
    x /= 10;
  }
  return false;
}

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

  int n, l; cin >> n >> l;
  int count = 0;
  int cur = 1;
  while (true) {
    if (!hasDigit(cur, l)) {
      count++;
      if (count == n) {
        cout << cur << "\n";
        return 0;
      }
    }
    cur++;
  }

  return 0;
}