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 OXOX-axis at point 00 and you want to move to an integer point x>0x>0.
You can make several jumps. Suppose you're currently at point yy (yy may be negative) and jump for the kk-th time. You can:
What is the minimum number of jumps you need to reach the point xx?
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The first and only line of each test case contains the single integer xx (1≤x≤1061≤x≤106) — the destination point.
Output
For each test case, print the single integer — the minimum number of jumps to reach xx. It can be proved that we can reach any integer point xx.
Example
input
Copy
5 1 2 3 4 5
output
Copy
1 3 2 3 4
Note
In the first test case x=1x=1, so you need only one jump: the 11-st jump from 00 to 0+1=10+1=1.
In the second test case x=2x=2. You need at least three jumps:
Two jumps are not enough because these are the only possible variants:
In the third test case, you need two jumps: the 11-st one as +1+1 and the 22-nd one as +2+2, so 0+1+2=30+1+2=3.
In the fourth test case, you need three jumps: the 11-st one as −1−1, the 22-nd one as +2+2 and the 33-rd one as +3+3, so 0−1+2+3=40−1+2+3=4.
=========================================================================
首先对于能够用 1+2+...n表示的,那么肯定首选n种
1+2+3+............+n = y
y ->n
y-1 y的基础之上减1
y-2 第一步变成-1
y-3 第二部变成-1
。。
y-n+1 第n-1部分变成-1
y-n 也就是1+2+3....+n-1 会被上一次循环拦截掉
- #include
- using namespace std;
- typedef long long int ll;
-
- int main()
- {
-
-
- int t;
-
- cin>>t;
-
- while(t--)
- {
- int n;
-
- cin>>n;
-
- for(ll i=1;;i++)
- {
- ll now=(i)*(i+1)/2;
-
- if(now==n)
- {
- cout<
- break;
- }
- else if(now==n+1)
- {
- cout<1<
- break;
- }
- else if(now>n)
- {
- cout<
- break;
-
- }
- }
- }
- return 0;
- }
-
相关阅读:
「随笔」浅谈2023年云计算的发展趋势
AOP基础&&进阶
React Native优质开源项目精选
java毕业设计房屋出租mybatis+源码+调试部署+系统+数据库+lw
轻量封装WebGPU渲染系统示例<36>- 广告板(Billboard)(WGSL源码)
API阶段测试
CocosCreator 面试题(十二)Cocos Creator Label 的原理以及如何减少Drawcall
湖仓一体(Lakehouse)架构的核心组件之存储层——Lakehouse 架构(三)
POSIX 真的不适合对象存储吗?
在 Windows 上开发.NET MAUI 应用_1.安装开发环境
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126196588
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU