이문제는 최대로 더해 질 수 있는수가 999999 로 9*6인 54이다.
그래서 54보다 작은 수에서 시작해서 brute force로 탐색해서 풀면 된다.
코드는 다음과 같다.
//분해합
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
int n = 0;
int nAns = 0;
int nLen = 0;
string strTmp;
cin >> n;
nLen = to_string(n).length();
for(int t = n - 54; t < n; t++) // 6 * maxnum(9) = 54
{
int nTmp = t;
int nCom = t;
for(int i = 0; i < nLen; i++)
{
nCom += (nTmp % 10);
nTmp /= 10;
}
if(nCom == n)
{
nAns = t;
break;
}
}
cout << nAns << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[백준 11060] 점프 점프 (0) | 2020.11.19 |
---|---|
[백준 19598] 최소 회의실 개수 (1) | 2020.11.01 |
[백준 4848] 집합 숫자 표기법 (0) | 2020.11.01 |
[백준 16953] A > B (2) | 2020.10.16 |
[백준 11729] 하노이 탑 이동 순서 (0) | 2020.10.15 |