天天看點

C - Go Home

There is a kangaroo at coordinate 00 on an infinite number line that runs from left to right, at time 00. During the period between time i−1i−1 and time ii, the kangaroo can either stay at his position, or perform a jump of length exactly ii to the left or to the right. That is, if his coordinate at time i−1i−1 is xx, he can be at coordinate x−ix−i, xx or x+ix+i at time ii. The kangaroo's nest is at coordinate XX, and he wants to travel to coordinate XX as fast as possible. Find the earliest possible time to reach coordinate XX.

Constraints

  • XX is an integer.
  • 1≤X≤1091≤X≤109

Input

The input is given from Standard Input in the following format:

XX
      

Output

Print the earliest possible time for the kangaroo to reach coordinate XX.

Sample Input 1 Copy

Copy

6
      

Sample Output 1 Copy

Copy

3
      

The kangaroo can reach his nest at time 33 by jumping to the right three times, which is the earliest possible time.

Sample Input 2 Copy

Copy

2
      

Sample Output 2 Copy

Copy

2
      

He can reach his nest at time 22 by staying at his position during the first second, and jumping to the right at the next second.

Sample Input 3 Copy

Copy

11
      

Sample Output 3 Copy

Copy

5      

題意:

在一個x軸上,開始你站在0的位置,第幾分鐘你就可以向左或向右走幾步,例如第i-1分鐘你在x位置,那麼第i分鐘你可以不動或走到x-i或x+i位置上。求你走到n位置最少要幾分鐘。

思路:

1: 1 

2 :3

3 :6

...........

從0一直往右走直到大于等于n。就是答案。因為如果大于n的話比n大多少你就可以在把走最後一步的時間(第幾分鐘的時候停一分鐘)。

代碼:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n;
    cin>>n;
    ll sum=0;
    for(int i=1;;i++)
    {
        sum+=i;
        if(sum>=n)
        {
            cout<<i<<endl;
            return 0;
        }
    }
}
           

繼續閱讀