[백준 4436] 엘프의 검 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
n의 배수들을 순서대로 보며 0~9가 모두 등장하는 가장 작은 k를 구하는 문제입니다.
접근법
k를 1부터 증가시키며 n의 k배에 해당하는 수의 자릿수를 확인합니다.
이후 등장한 숫자를 표시하고 0부터 9까지 모두 등장하면 그 k를 출력합니다.
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
using System;
class Program {
static void Main() {
string? line;
while ((line = Console.ReadLine()) != null) {
line = line.Trim();
if (line.Length == 0) continue;
var n = int.Parse(line);
var seen = new bool[10];
var cnt = 0;
var k = 0;
while (cnt < 10) {
k++;
var x = (long)n * k;
if (x == 0) {
if (!seen[0]) { seen[0] = true; cnt++; }
} else {
while (x > 0) {
var d = (int)(x % 10);
if (!seen[d]) { seen[d] = true; cnt++; }
x /= 10;
}
}
}
Console.WriteLine(k);
}
}
}
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string line;
while (cin >> line) {
int n = stoi(line);
bool seen[10] = {};
int cnt = 0;
int k = 0;
while (cnt < 10) {
k++;
ll x = 1LL * n * k;
if (x == 0) {
if (!seen[0]) { seen[0] = true; cnt++; }
} else {
while (x > 0) {
int d = x % 10;
if (!seen[d]) { seen[d] = true; cnt++; }
x /= 10;
}
}
}
cout << k << "\n";
}
return 0;
}