[Programmers] N으로 표현

Time Lapse :10min 2sec

solution.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
#include <math.h>
#include <algorithm>
using namespace std;

int DFS(int N,int number,long long cur,int used){
if(used>=9)
return 987654321;
if(cur==number)
return used;
int ans = 987654321;
int a = 0;
for(int i=1;i<(9-used);i++){
a += pow(10,i-1);
int need = N*a;

ans = min(ans,DFS(N,number,cur+need,used+i));
ans = min(ans,DFS(N,number,cur-need,used+i));
ans = min(ans,DFS(N,number,cur/need,used+i));
ans = min(ans,DFS(N,number,cur*need,used+i));
}
return ans;
}

int solution(int N,int number){
int ans = DFS(N,number,0,0);
ans = ans > 8 ? -1 : ans;
return ans;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/expressN/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.