Educational Codeforces Round 99 (Rated for Div. 2)------Jumps

题目

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.

题意

你当前在原点,你第k次跳跃距离为k,意思就是当前位置向前走k个距离或者只能后退一个单位的距离,问你最少需要多少次才能到达x

题解

找出第一个满足这个式子的值n,1 + 2 + , + n = s >= x
1、如果s == x,输出n即可
2、如果s > x + 1 && s < x + n, 那么可以找出一个一个小于n的数,变成-1,那个么x就和s相等了,答案即是n
3、如果s == x + 1, 那么就需要其中一个数字贡献一次-1,然后变为2的情况,答案即是n + 1

AC代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int t; cin >> t;
	while (t--) {
		int x; cin >> x;
		int sum = 0, n = 0;
		for (int i = 1;; i++) {
			if (sum >= x)
				break;
			sum += i;
			n++;
		}
		if (sum == x)
			cout << n << endl;
		else if (sum == x + 1)
			cout << n + 1 << endl;
		else
			cout << n << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值