-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoodMorning.cpp
72 lines (58 loc) · 1.17 KB
/
GoodMorning.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
#include <bits/stdc++.h>
#define input freopen("in.txt", "r", stdin)
using namespace std;
vector<int> posibles[] =
{
{},
{2,4},
{3,5},
{6},
{5,7},
{6,8},
{9},
{8},
{9,0},
{},
};
int k;
int min_diff;
int cercano;
int cont;
void backtracking(int actual, int acc){
cont++;
// printf("it %d: %d %d\n",cont,actual,acc);
// if(cont>1000)
// exit(0);
if (abs(k-acc)<min_diff)
{
cercano = acc;
min_diff = abs(k-acc);
// printf("min: %d\n",min_diff);
}
int nuevo = acc*10+actual;
// printf("nuevo: %d\n",nuevo);
// return;
if (acc*10-k>min_diff || actual+acc== 0 )
return;
backtracking(actual,nuevo);
for (int i = 0; i < posibles[actual].size(); i++)
{
backtracking(posibles[actual][i],acc);
}
}
int main() {
input;
int t;
scanf("%d",&t);
for (int i = 0; i < t; i++)
{
min_diff=200;
cercano=0;
cont=0;
scanf("%d",&k);
backtracking(1,0);
printf("%d\n",cercano);
// printf("it: %d\n",cont);
}
return 0;
}