-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNexpress.cpp
More file actions
52 lines (38 loc) · 736 Bytes
/
Nexpress.cpp
File metadata and controls
52 lines (38 loc) · 736 Bytes
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
#include <iostream>
#include <string>
using namespace std;
int target;
int value;
int min(int n1, int n2)
{
if(n1 < n2)
return n1;
return n2;
}
int search(int n, int count)
{
int answer = 987654321;
if(count >= 9)
return 9;
if(n == target)
return count;
answer = search(n + value, count + 1);
answer = min(search(n * value, count + 1), answer);
answer = min(answer, search(n / value, count + 1));
string s = to_string(n);
int len = 1;
for(int i = 0; i < s.size(); ++i)
len *= 10;
answer = min(answer , search(n * len + value, count + 1));
return answer;
}
int main(void)
{
int n;
cin >> n;
for(int test = 0; test < n; ++test)
{
cin >> value >> target;
cout << search(value, 1) << endl;
}
}