본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 2231 분해합

by 왹져박사 2023. 10. 18.
728x90

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

 

2231번: 분해합

어떤 자연수 N이 있을 때, 그 자연수 N의 분해합은 N과 N을 이루는 각 자리수의 합을 의미한다. 어떤 자연수 M의 분해합이 N인 경우, M을 N의 생성자라 한다. 예를 들어, 245의 분해합은 256(=245+2+4+5)이

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace _2231
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int n = int.Parse(sr.ReadLine());
            int m = 0;
            int sum = 0;
            for (int i = 1; i < n; i++)
            {
                int num = i;
                sum = num;
                while (true)
                {
                    if (num == 0) break;
                    sum += num % 10;
                    num /= 10;
                }
                if (sum == n)
                {
                    m = i;
                    break;
                }
            }
            sw.Write(m);
            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

728x90