1455B Jumps(思维、规律)

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 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.

思路:先假设一直相加:1+2+3+4+5+6+…+k=sum,
这时通过观察可以发现假如第一步我选择退后则总和为
-1+2+3+4…+k=sum-2,假设在第二步倒退,1-1+3+4+…+k=sum-3,依此类推,我们可以得出我们从一一直加直到>=n(题目的输入),如果刚好=n,答案为k,如果sum-1==n,答案为g+1,我们只有在最后一步倒退可得,步数+1,如果sum>n,答案为g,我们将之前某一步设为到退即可。

Code

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<stack>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 2e5 + 5;
int lst[Max];
int Mod = 1e9 + 7;

int main()
{
   
	FAST;
	int t;cin >> t;
	while (t--)
	{
   
		int n;cin >> n;
		int i = 0, g = 0;
		while (i < n)
		{
   
			i += (++g);
		}
		if (i == n)cout << g << endl;
		else if (i - 1 == n)cout << g + 1 << endl;
		else cout << g << endl;
	}
}

全部评论

相关推荐

06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务