C# 2231번 분해합

2022. 1. 10. 13:19C#/[백준] 브루트 포스

문제

코드 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2
{
    class Program
    {
        static void Main(string[] args)
        {
            string N = Console.ReadLine();
            int num = 1;

            int constructor = 0;
            while(Convert.ToInt32(N) > num)
            {
                constructor = num;
                for (int i = 0; i < num.ToString().Length; i++)
                {
                    constructor += Convert.ToInt32(num.ToString()[i].ToString());
                }
                if(Convert.ToInt32(N) == constructor)
                {
                    break;
                }
                num += 1;
            }
            if (Convert.ToInt32(N) == num)
                num = 0;
            
            Console.WriteLine(num);
        }
    }
}

코드 풀이 :

1부터 1씩 더해가면서 각 자리수 별로 더해서 입력값과 같은 수를 찾는다.

입력값보다 1씩 더한 값이 커지면 break를 하고 0을 출력한다.

깃허브 :

https://github.com/CheongHo-Lee/Algorithm-Study

 

GitHub - CheongHo-Lee/Algorithm-Study: Algorithm Study For Coding Test

Algorithm Study For Coding Test. Contribute to CheongHo-Lee/Algorithm-Study development by creating an account on GitHub.

github.com

 

'C# > [백준] 브루트 포스' 카테고리의 다른 글

C# 1436번 영화감독 숌  (0) 2022.01.10
C# 1018번 체스판 다시 칠하기  (0) 2022.01.10
C# 7568번 덩치  (0) 2022.01.10
C# 2798번 블랙잭  (0) 2022.01.10