백준 algorithm
백준 2231 - 분해합
cosmohoo
2019. 9. 10. 14:33
반응형
간단하게 브루트포스로 구하면 되는 문제였다.
자릿수를 세는 과정에서 실수가 있었고, 벡터에 넣는 값을 다른 값을 집어넣는 실수를 하여 코드가 깔끔하지가 않다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int N = 0;//분해합의 수
vector<int> M; //생성자 집합
cin >> N;
for (int i = 1; i < N; i++)
{
int inum = i;//숫자를 임시로 저장할 변수
int temp = i;
int jari = 1;
int jari_num = 0;
while (inum >= jari)//숫자의 자릿수를 세는 while문
{
jari *= 10;
jari_num++;
}
for (int j = 0; j < jari_num; j++)//각자릿수를 temp변수에 더하는 for문
{
temp += inum % 10;
inum = inum / 10;
}
if (temp == N)
{
M.push_back(i);//조건에 맞는 숫자를 vector에 집어 넣음
}
}
if (M.size() == 0)cout << 0;//생성자가 없는 경우
else{
sort(M.begin(), M.end());//순서대로 들어가기에 필요없는 코드
cout << *(M.begin());
}
return 0;
}
반응형