Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
관리 메뉴

dh-winternagi 님의 블로그

(2231) 분해합 본문

백준 (C++)/Solve

(2231) 분해합

dh-winternagi 2026. 4. 13. 21:42

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

단계별로 풀어보기

12단계(브루트 포스) 2번째

 

 

 

단순한 브루트 포스 말고 가지치기를 하거나 다른 알고리즘을 사용해 더욱 최적화가 가능한 문제도 있지만 나중에 높은 랭크의 문제에서 자연스럽게 하게 될 것이므로 굳이 할 필요는 없을 것 같다.

 

 

 

#include <iostream>
using namespace std;

int dsum(int x){
  int res= x;
  
  while(x){
    res+= x%10;
    x/= 10;
  }
  
  return res;
}

int main() 
{
  int n;
  
  cin >> n;
  
  for(int i=1;i<=n;i++){
    if(dsum(i)==n){
      cout << i;
      return 0;
    }
  }
  
  cout << 0;
  
  return 0;
}