-
Notifications
You must be signed in to change notification settings - Fork 0
/
리모컨.cpp
147 lines (130 loc) · 2.47 KB
/
리모컨.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <cmath>
using namespace std;
int digitIs(int num)
{
if (num == 0)
return 1;
int digit = 0;
while (num != 0)
{
num = num / 10;
digit++;
}
return digit;
}
bool isSame(int num, int* broken, int m) //if broken, return true
{
for (int i = 0; i < m; i++)
{
if (num == broken[i])
return true;
}
return false;
}
void numToArr(int input, int digit, int *arr)
{
for (int i = 0; i < digit; i++)
{
arr[digit - i - 1] = input % 10;
input = input / 10;
}
}
int arrToNum(int digit, int arr[])
{
int result = 0;;
int newDigit = digit;
for (int i = 0; i < digit; i++)
{
result = result + arr[digit - newDigit] * (int)pow(10, newDigit - 1);
newDigit--;
}
return result;
}
int sub(int num1, int num2)
{
if (num1 > num2)
return num1 - num2;
if (num1 < num2)
return num2 - num1;
if (num1 == num2)
return 0;
}
int main(void)
{
int n, m, count, digit;
cin >> n >> m;
digit = digitIs(n); //채널의 자리수
int *broken, *notBroken;
broken = new int[m];
notBroken = new int[10 - m];
for (int i = 0; i < m; i++)
cin >> broken[i];
int j = 0;
for (int i = 0; i < 10; i++)
{
if (!isSame(i, broken, m))
{
notBroken[j] = i;
j++;
}
}
int *inputToArr = new int[digit];
numToArr(n, digit, inputToArr); //inputToArr에 n을 자리수별로 쪼개서 저장
int *newChan1 = new int[digit];
int *newChan2 = new int[digit];
for (int i = 0; i < digit; i++)
{
if (inputToArr[i] < notBroken[0])
{
for (int k = i; k < digit; k++)
{
newChan1[k] = notBroken[0];
newChan2[k] = notBroken[0];
}
break;
}
if (inputToArr[i] > notBroken[10 - m - 1])
{
for (int k = 0; k < digit; k++)
{
newChan1[k] = notBroken[10 - m - 1];
newChan2[k] = notBroken[10 - m - 1];
}
break;
}
for (int j = 0; j < 10 - m; j++)
{
if (notBroken[j] == inputToArr[i]) //같으면 집어넣기
{
newChan1[i] = notBroken[j];
newChan2[i] = notBroken[j];
break;
}
if (notBroken[j] > inputToArr[i])
{
newChan1[i] = notBroken[j];
newChan2[i] = notBroken[j - 1];
}
}
if (newChan1[i] != newChan2[i])
{
for (int k = i + 1; k < digit; k++)
{
newChan1[k] = notBroken[10 - m - 1];
newChan2[k] = notBroken[0];
}
break;
}
}
if (sub(arrToNum(digit, newChan1), n) > sub(arrToNum(digit, newChan2), n))
count = digit + sub(arrToNum(digit, newChan2), n);
else
count = digit + sub(arrToNum(digit, newChan1), n);
if (m == 0)
count = digit;
if (n == 100)
count = 0;
cout << count;
return 0;
}