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

2022.02.03 [백준] C# 분해합

by ian's coding 2022. 2. 3.
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


풀이

 
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
31
32
using System;
using System.Text;
 
 
class Program
{
    
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int result = 0;
        while (result<=n)
        {
            if (result == n)
            {
                Console.WriteLine(0);
                break;
            }
            if (result + (result / 100000) % 10 +(result / 10000) % 10 + (result / 1000) % 10
                 + (result / 100) % 10 + (result / 10) % 10 + result % 10 == n)
            {
                Console.WriteLine(result);
                break;
            }
            else
            {
                result++;
            }
        }
        
    }
}
cs

각 자리의 수를 구하기 위해 나머지와 몫을 이용해 구했음.

 

 

728x90
반응형

댓글