-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1107.js
56 lines (47 loc) · 1.4 KB
/
1107.js
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
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
const list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
function main(input) {
const [N, M, ..._] = input;
const b_list = _.length !== 0 ? _[0].split(' ') : [];
b_list.forEach(b => {
list[+b] = false;
});
let i = 0;
let basic = Math.abs(N - 100);
while (true) {
if (M === '10') { console.log(basic); return; }
if(basic < i) {console.log(basic); return;}
if ((Number(N) - i) >= 0) {
if (checkArr((Number(N) - i).toString().split(''))) {
let up = (Number(N) - i);
let upCase = up.toString().split('').length + Math.abs(i);
console.log(Math.min(upCase, basic));
return;
}
}
if (checkArr((Number(N) + i).toString().split(''))) {
let down = (Number(N) + i);
let downCase = down.toString().split('').length + Math.abs(i);
console.log(Math.min(downCase, basic));
return;
}
i++
}
}
function checkArr(arr) {
for (let i = 0; i < arr.length; i++) {
if (list[+arr[i]] === false) return false;
}
return true;
}