Go Home(UPC)

Go Home(UPC)

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

Constraints
X is an integer.
1≤X≤1e9
*

input

6

output

3

*思路:
情况1:当第i点时恰好走到x,为最佳状态,此时直接输出i;
情况2:当第i点时恰好超过x,当第i-1点时恰好小于x;直接输出i就可;
因为假设第i点走了s(s>x)则我可以在第(s-x)点时不动(少走了(s-x)),恰好补回了那点差距,则可以在第i点直接到x位置;

#include<bits/stdc++.h>
using namespace std;
int main(){
	int x,i,t;
	cin>>x;
	for(i=1;;i++) {
		t=(i*(i+1))/2;
		if(t>=x) break;
	}
	cout<<i;
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nap!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值