관리 메뉴

cyphen156

백준-브루트 포스 2231 분해합 본문

컴퓨터공학/알고리듬 풀이

백준-브루트 포스 2231 분해합

cyphen156 2025. 4. 1. 12:08

분해합

 

분해합 : N과 N을 이루는 각 자리수의 합 (245 => 245 + 2 + 4 + 5 == 256)

생성자 :  M의 분해합이 N인 경우

가장 작은 생성자를 구하라

제약사항

  • 0 < N <= 1,000,000
  • if No Result, returns 0

주의 사항

없다.

CPP풀이

분해합_2231.cpp

/**
 * 백준 분해합_2231
 * 분해합 : N과 N을 이루는 각 자리수의 합 (245 => 245 + 2 + 4 + 5 == 256)
 * 생성자 :  M의 분해합이 N인 경우
 * 분해합이 주어졌을 때 가장 작은 생성자를 구하라
 * 
 * 제한사항
 *****************************************
 * 0 < N <= 1,000,000                    *
 * if No Result, returns 0               *
 *****************************************
 *
 *
 *
 * 주의
 * 없다.
 * 
 * 풀이시간 30분
 */


#include <iostream>

using namespace std;

int main(void)
{
    bool isConstructor = false;
    int N;

    cin >> N;
    int idx = 0;
    for (idx = 1; idx < N; idx++) {
        int sum = idx;
        int temp = idx;

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

        if (sum == N) {
            isConstructor = true;
            break;
        }
    }
    if (isConstructor)
    {
        cout << idx << endl;
    }
    else 
    {
        cout << 0 << endl;
    }
    return 0;
}

모든 예제 코드의 소스파일은 제 개인 깃허브 레포지토리 에 있습니다.

 

Workspace/알고리듬 풀이 at main · cyphen156/Workspace

Studying . Contribute to cyphen156/Workspace development by creating an account on GitHub.

github.com