백준 알고리즘
[백준 알고리즘] 1789번 수들의 합
ChocoPeanut
2017. 5. 17. 17:03
수들의 합
서로 다른 N개의 자연수의 합이 S라고 한다. S를 알 때, 자연수 N의 최대값은 얼마일까?
#include<iostream>
using namespace std;
int main()
{
long long S;
cin >> S;
long long x=0;
long long num=1;
while (x < S)
{
x = x + num;
num++;
}
if (x == S)
{
cout << num - 1 << endl;
}
else {
cout << num - 2 << endl;
}
}