B. Jumps

传送门

1455 B. Jumps

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are standing on the OX-axis at point 0 and you want to move to an integer point x>0 You can make several jumps. Suppose you’re currently at point y(y may be negative) and jump for the k-th time. You can: either jump to the point y+k or jump to the point y−1 What is the minimum number of jumps you need to reach the point x?

Input
The first line contains a single integer t(1≤t≤1000) — the number of test cases.The first and only line of each test case contains the single integer x(1≤x≤106) — the destination point.

Output
For each test case, print the single integer — the minimum number of jumps to reach x It can be proved that we can reach any integer point x

Example
Input

5
1
2
3
4
5

Output

1
3
2
3
4

思想:先累加,后进行判断,只有三种情况,第一累加的结果等于n,则输出累加的步数;第二累加的结果等于n+1,则输出累加的步数加1;第三不满足前两项,但大于n,则输出累加的步数。

AC代码

#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{

    ll int i,t,n,sum;
    cin>>t;
    while(t--)
    {
     sum=0;   cin>>n;
        for(int i=1;i<=n;i++)
        {
            sum+=i;
            if(sum>=n)
               {
                   if(sum==n)
                    {cout<<i<<endl;break;}
                   else if(sum==n+1)
                    {cout<<i+1<<endl;break;}
                   else if(sum>n)
                    {cout<<i<<endl;break;}
        }
        }}return 0;
    }

Note
In the first test case x=1, so you need only one jump: the 1-st jump from 0 to 0+1=1 In the second test case x=2. You need at least three jumps:
the 1-st jump from 0 to 0+1=1;the 2-nd jump from 1 to 1+2=3;the 3-rd jump from 3 to 3−1=2 ;Two jumps are not enough because these are the only possible variants: the 1-st jump as −1 and the 2-nd one as −1 — you’ll reach 0−1−1=−2;the 1-st jump as −1 and the 2-nd one as +2 — you’ll reach 0−1+2=1;the 1-st jump as +1 and the 2-nd one as −1 — you’ll reach 0+1−1=0;the 1-st jump as +1 and the 2-nd one as +2 — you’ll reach 0+1+2=3 ; In the third test case, you need two jumps: the 1-st one as +1 and the 2-nd one as +2, so 0+1+2=3.In the fourth test case, you need three jumps: the 1-st one as −1, the 2-nd one as +2 and the 3-rd one as +3, so 0−1+2+3=4

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

稚皓君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值