본문 바로가기

완전 탐색 (Brute Force)

백준 2231 분해합

문제 링크: https://www.acmicpc.net/problem/2231

 

2231번: 분해합

문제 어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이 된다. 따라서 245는 256의 생성자가 된다. 물론, 어떤 자연수의 경우에는 생성자가 없을 수도 있다. 반대로, 생성자가 여러 개인 자연수도 있을 수 있다. 자연수 N이 주어졌을 때, N의 가장 작은 생성자를 구해내는 프로그

www.acmicpc.net

1부터 n까지 훑으면서 모든수에 대해 분해합을 구한후, 그 분해합이 n과 같을때의 수를 출력한다.

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 <bits/stdc++.h>
using namespace std;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;
 
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        int num = i;
        int sum = i;
        while(num) {
            sum += num % 10;
            num /= 10;
        }
        if(sum == n) {
            ans = i;
            break;
        }
    }
 
    cout << ans << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

재귀함수를 사용해서도 구현해봤다.

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
#include <bits/stdc++.h>
using namespace std;
 
int dsum(int num) {
    if(num == 0return 0;
    return num % 10 + dsum(num / 10);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;
 
    for(int i = 1; i <= n; i++) {
        if(i + dsum(i) == n) {
            cout << i << '\n';
            return 0;
        }
    }
    cout << 0 << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

재귀함수를 연습할겸 인자를 조금 바꿔서도 구현해봤다.

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
#include <bits/stdc++.h>
using namespace std;
 
int dsum(int num, int sum) {
    if(num == 0return sum;
    return dsum(num / 10, sum + num % 10);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;
 
    for(int i = 1; i <= n; i++) {
        if(dsum(i, i) == n) {
            cout << i << '\n';
            return 0;
        }
    }
    cout << 0 << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

재귀함수가 bool값을 리턴하게끔도 바꿔서 구현해봤다.

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 <bits/stdc++.h>
using namespace std;
 
bool dsum(int num, int sum, int target) {
    if(num == 0) {
        if(sum == target) return true;
        return false;
    }
    return dsum(num / 10, sum + num % 10, target);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    int n;
    cin >> n;
 
    for(int i = 1; i <= n; i++) {
        if(dsum(i, i, n)) {
            cout << i << '\n';
            return 0;
        }
    }
    cout << 0 << '\n';
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

'완전 탐색 (Brute Force)' 카테고리의 다른 글

백준 1018 체스판 다시 칠하기  (0) 2020.04.24
백준 2503 숫자 야구  (0) 2020.04.24
백준 10448 유레카 이론  (0) 2020.04.23
백준 3085 사탕 게임  (0) 2020.04.23
백준 2309 일곱 난쟁이  (0) 2020.04.23