[백준] 1789번 수들의 합 / C++
#문제
#풀이
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false); cin.tie(nullptr);
long long s;
cin >> s;
int n = 0;
while (s > 0)
{
n++;
s = s - n;
}
if (s < 0)
{
n--;
}
cout << n;
return 0;
}
#정리
서로 다른 자연수의 가장 큰 수를 구해야 하기 때문에 무조건 1부터 더한다. s를 초과할 경우, 연산을 종료하며 s보다 크다면 n– 했을 때가 가장 큰 수이다. s에서 n++를 빼주고, s가 0보다 작다면 n–를 하여 n을 출력하여 해결.
Enjoy Reading This Article?
Here are some more articles you might like to read next: