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;
}
posted @ 2020-12-01 14:00  AC_沫离  阅读(30)  评论(0)    收藏  举报