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
#include <string>
#include <vector>
#include <set>
using namespace std;
#define INF 987654321
 
 
int solution(int N, int number) {
    int answer=-1;
    set<int> s[9];
    
    int sum=0;
    for(int i=1;i<=7;i++){
        sum=sum*10+N;
        s[i].insert(sum);
    }
 
 
    for(int i=2;i<=8;i++){
        for(int j=1;j<i;j++){
            for(int a : s[j]){
                for(int b : s[i-j]){
                    s[i].insert(a+b);
                    s[i].insert(a*b);
                    if(a-b>0)
                        s[i].insert(a-b);
                    if(b!=0)
                        s[i].insert(a/b);
                }
            }
        }
    }
    for(int i=1;i<=8;i++){
        if(s[i].find(number)!=s[i].end()){
            answer=i;
            break;
        }
    }
    
    
    return answer;
    
}
cs

출처: https://school.programmers.co.kr/learn/courses/30/lessons/42895

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

s[i]: 숫자 N을 i번 사용한 숫자들의 집합을 저장.

ex) s[4]= s[i] 사칙연산  s[j] (i+j==4) 

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[c++] 경주로 건설 (다익스트라)  (0) 2022.10.09
[c++] 무지의 먹방 라이브  (0) 2022.10.08
[c++] 아이템줍기 (BFS)  (1) 2022.09.29
[c++] 1차 비밀지도 (구현)  (0) 2022.09.28
[c++] 베스트앨범 (해쉬)  (0) 2022.09.23

+ Recent posts