브루트 포스(Brute Force)
백준 2231번: 분해합
cepiloth
2018. 6. 13. 19:28
반응형
https://www.acmicpc.net/problem/2231
1.문제 요약
어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이 된다. 따라서 245는 256의 생성자가 된다. 물론, 어떤 자연수의 경우에는 생성자가 없을 수도 있다. 반대로, 생성자가 여러 개인 자연수도 있을 수 있다. 자연수 N이 주어졌을 때, N의 가장 작은 생성자를 구해내는 프로그램을 작성하시오.
2. 알고리즘
노가다로 풀었습니다.
ㅜㅜ
3. 코드
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 | #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <functional> // greater 사용 위해 필요 #include <string> #include <map> #include <math.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for(int i =1; i <= 1000000; i++) { int cand = i; int createNumber = 0; createNumber += cand; while (cand != 0) { int remain = cand % 10; createNumber += remain; cand /= 10; } if (createNumber == n) { cout << i << "\n"; break; } if (i+1 > 1000000) { cout << "0" << "\n"; } } return 0; } | cs |
반응형