概述
codeforces 1455 B
2021-01-14 打卡题,随便在codeforces上找了一道
B. Jumps
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
题目描述:
You are standing on the OX-axis at point 0 and you want to move to an integer point ????>0.
You can make several jumps. Suppose you’re currently at point ???? (???? may be negative) and jump for the ????-th time. You can:
either jump to the point ????+????
or jump to the point ????−1.
What is the minimum number of jumps you need to reach the point ?????
Input
The first line contains a single integer ???? (1≤????≤1000) — the number of test cases.
The first and only line of each test case contains the single integer ???? (1≤????≤106) — the destination point.
Output
For each test case, print the single integer — the minimum number of jumps to reach ????. It can be proved that we can reach any integer point ????.
Example:
inputCopy
5
1
2
3
4
5
outputCopy
1
3
2
3
4
Note:
In the first test case ????=1, so you need only one jump: the 1-st jump from 0 to 0+1=1.
In the second test case ????=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.
思路分析:
这个题,画出对应的树,从根结点开始往下依次计算,画到大概5-6层的时候,规律已经很明显了,建议自己去找一找。
代码实现:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--) {
int n;
scanf("%d",&n);
int sum = 0;
int i;
for(i=1;i<=n;i++) {
sum += i;
if(sum >= n)
break;
}
if(n != sum-1)
printf("%dn",i);
else
printf("%dn",i+1);
}
return 0;
}
最后
以上就是饱满电脑为你收集整理的codeforces 1455 B题的全部内容,希望文章能够帮你解决codeforces 1455 B题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复