[C#] 백준 2292번 벌집

2021. 12. 21. 21:22C#/[백준] 기본 수학1

문제

정답 코드 :

using System;
using System.Collections.Generic;
namespace _2
{
    class Program
    {
        static void Main(string[] args)
        {
            int input = Convert.ToInt32(Console.ReadLine());
            int n = 1;
            int k = n - 1;
            int output = 1;
                
            while (input > output)
            {
                output +=  6 * (n-1); 
                n++;
            }
            
            if (input == 1)
                Console.WriteLine(n);
            else
                Console.WriteLine(n-1);
        }
    }
}

이 문제에는 규칙이 있다 바로 값들의 공차가 일정한 계차수열인 것이다.

 

따라서 계차수열을 구해주는 코드를 추가하고 그 값이 Input보다 커지면 해당 n을 구하면 된다.

while (input > output)
{
	output +=  6 * (n-1); 
	n++;
}

예를 들어 6은 7보다 작은 수 이기때문에 2다. 38은 61보다 작기 때문에 5다.