Little Jay

[C++] 백준 2231번 분해합 본문

알고리즘/BOJ

[C++] 백준 2231번 분해합

Jay, Lee 2021. 5. 3. 13:34

Brute Force 알고리즘 문제

 

처음에 문제 이해 못해서 엄청 고생했지만

결국 분해합의 생성자는 입력된 값 보다 작다는거를

명심하고 풀기

 

#include <iostream>
using namespace std;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int num, sum, temp;
	cin >> num;

	for (int i = 1; i < num; ++i) {
		sum = i;
		temp = i;

		while (temp) {
			sum += temp % 10;
			temp /= 10;
		}

		if (num == sum) {
			cout << i << endl;
			return 0;
		}
	}

	cout << 0 << endl;

	return 0;
}

Comments