codeforces 11 B.Jumping Jack 想法题
2016-05-25 21:33
435 查看
B. Jumping Jack
Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
Input
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output
Output the minimal number of jumps that Jack requires to reach x.
Examples
input
output
input
output
input
output
Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
Input
The input data consists of only one integer x ( - 109 ≤ x ≤ 109).
Output
Output the minimal number of jumps that Jack requires to reach x.
Examples
input
2
output
3
input
6
output
3
input
0
output
0 题意:给你一个点,你从0开始走,每次你可以选择向左或者向右走,从1开始每次必须加一步,问你走到那个点的最小步数; 思路:小于0的话就改成大于0的;首先那些步数相加一定要大于那个点的位置;和减去那个点的位置相差为偶数的话就可以到达,从小判断,详见代码;
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 #define pi 4*atan(1) //#pragma comment(linker, "/STACK:102400000,102400000") int a[100010]; int main() { int x,y,z,i,t; for(i=0;i<50000;i++) { if(i%2==0) a[i]=i/2*(i+1); else a[i]=(i+1)/2*i; } while(~scanf("%d",&x)) { if(x<0)x=-x; for(i=0;;i++) if(a[i]>=x&&(a[i]-x)%2==0) break; printf("%d\n",i); } return 0; }
相关文章推荐
- Scrum 项目 7.0
- windows7使用Source insight上远程修改ubuntu共享内核源码
- solr使用cursorMark做深度分页
- IOの随机流
- 隐写分析RS算法
- MapReduce入门学习心得
- hdu1495~广搜
- Linux及安全实践二
- Scrum项目7.0
- scrapy开发三之开发过程中的那些经常性错误
- CentOS下LVS DR模式详细搭建过程
- linux进程管理
- CodeForces 651B Beautiful Paintings
- BZOJ 1951: [Sdoi2010]古代猪文
- Qt多窗口切换程序
- 图解闭包
- OJ提交题目中的语言选项里G++与C++的区别(转)
- 执行maven -v报错UnsupportedClassVersionError
- Eclipse快捷键 10个最有用的快捷键
- 你需要知道的 Android 拍照适配方案