Repository navigation
Expand file tree
/
Copy path24268.c
More file actions
83 lines (72 loc) · 1.5 KB
/
Copy path24268.c
File metadata and controls
83 lines (72 loc) · 1.5 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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char result[10];
int smallest = 1000000000;
int d_to_ten(char *result, int d)
{
int ten;
int arr[9] = {0};
for (int i = 0; i < d; i++)
arr[i] = (int)pow(d, i);
ten = 0;
for (int i = 0; i < d; i++)
ten += (result[d - i - 1] - '0') * arr[i];
return (ten);
}
// int permutation(int n, int r, int depth)
// {
// int smallest;
// if ()
// }
void swap(char *a, char *b)
{
char temp;
temp = *a;
*a = *b;
*b = temp;
}
void generatePermutations(char *str, int start, int end, int d)
{
if (start == end)
{
if (d_to_ten(str, d) < smallest)
smallest = d_to_ten(str, d);
}
else
{
for (int i = start; i <= end; i++)
{
// 현재 위치의 문자와 다음 위치의 문자를 교환
swap(&str[start], &str[i]);
// 다음 위치의 순열을 생성하기 위해 재귀 호출
generatePermutations(str, start + 1, end, d);
// 다시 원래대로 복원
swap(&str[start], &str[i]);
}
}
}
int main(void)
{
int n;
int d;
scanf("%d %d", &n, &d);
printf("0");
// result = malloc(sizeof(9));
// if (!result)
// exit(1);
// result = "000000000";
result[0] = '1';
result[1] = '0';
for (int i = 2; i < d; i++)
result[i] = i + '0';
result[d] = NULL;
printf("0");
generatePermutations(result, 0, n - 1, d);
if (smallest == 100000000)
printf("-1\n");
else
printf("%d\n", smallest);
return (0);
}