Programing/백준, 프로그래머스(C++)

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

hye3193 2025. 1. 3. 17:22

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

 

제출 코드

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

int getSum(int a)
{
    int sum = a;
    string str = to_string(a);
    for (int i = 0; i < str.length(); i++)
    {
        sum += str[i] - '0';
    }
    return sum;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    for (int i = 1; i < n; i++)
    {
        if (getSum(i) == n)
        {
            cout << i;
            return 0;
        }
    }

    cout << 0;
}