카테고리 없음

[BOJ/C#] 1789 수들의 합

일등하이 2023. 1. 28. 21:58
반응형

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

 

1789번: 수들의 합

첫째 줄에 자연수 S(1 ≤ S ≤ 4,294,967,295)가 주어진다.

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace BOJ
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
            StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));

            //n의 최대 : 가장 작은 수부터 더해서 s가 될 경우
            
            long n = long.Parse(sr.ReadLine());
            long s = 0;
            int cnt = 0;
            for (int i = 1; s < n; i++)
            {
                if (s >= n)
                    break;

                s += i;
                cnt++;
            }
            
            //sw.WriteLine(s);
            
            if( s > n)
                sw.WriteLine(cnt-1);    //넘치면 마지막에 더한수(하나) 빼면 댐
            else
                sw.WriteLine(cnt);


            sr.Close();
            sw.Close();


        }
        
        
    }
}
반응형