-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111111.cpp
More file actions
97 lines (97 loc) · 2.05 KB
/
111111.cpp
File metadata and controls
97 lines (97 loc) · 2.05 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <vector>
using namespace std;
vector<int> num;
int n_digit; //입력받은 수를 d진법 변환 시 차지하는 자리수
bool halt_flag = false; //불가능할 시 작동하는 flag
void dec_to_d(int n, int d)
{
int j = 0;
for (int i = n; i > 0; i /= d)
{
num[j++] = (i % d);
}
n_digit = j;
}
void calc(int n, int d)
{
int *getnum = new int[d];
int i = 0;
while (1)
{
for (int j = 0; j < d; j++)
getnum[j] = 0;
//저장된 숫자에서 1을 더한 수를 num에 저장되도록 함
i = 0;
num[i]++;
//자리수 올림(carryout)
while (num[i] == d)
{
num[i] = 0;
if (i + 1 == d)
{
halt_flag = true;
break;
}
num[i + 1]++;
i++;
}
if (halt_flag)
break;
//0~d-1의 모든 수가 한번씩 등장했다면 loop escape
int j;
for (j = 0; j < d; j++)
{
if (getnum[num[j]] > 0)
break;
else
getnum[num[j]]++;
}
if (j == d)
break;
}
delete[] getnum;
return;
}
int pow(int d, int k)
{
int mul = 1;
while (k--)
{
mul *= d;
}
return mul;
}
int d_to_dec(int d)
{
int ans = 0;
for (int i = 0; i < d; i++)
ans += pow(d, i) * num[i];
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int n, d;
cin >> n >> d;
num.assign(50, 0);
//d진법 변환
dec_to_d(n, d);
//변환한 수의 자리수가 d보다 클 경우 불가능하므로 -1 출력
if (n_digit > d)
{
cout << "-1\n";
return 0;
}
//문제의 답에 해당하는 d진법의 수를 찾는다.
calc(n, d);
if (halt_flag)
{
cout << "-1\n";
return 0;
}
//d진법의 답을 10진수로 바꾸어 출력한다.
cout << d_to_dec(d) << '\n';
return 0;
}