알고리즘

C++ 알고리즘 - 백준 2231 분해합

마루설아 2024. 12. 15. 00:37

https://www.acmicpc.net/problem/2231

 

#include <iostream>
#include <string>
using namespace std;

int main(void) {
	// C++ Init
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int in1;
	int num;
	cin >> in1;

	for (int i = 1; i <= 1000000; i++) {
		string str = to_string(i);
		int len = str.length();
		num = 0;

		for (int j = 0; j < len; j++) {
			num += str[j] - '0';
		}

		if (i + num == in1) {
			cout << i;
			return 0;
		}
	}

	cout << 0;
}