时间限制:C/C++语言 1000MS;其他语言 3000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB
题目描述:
小B对脑筋急转弯问题很有兴趣,她觉得这种问题的挑战能够反映一个人的应急反应能力。她正在开发一个智力测试的游戏,游戏的主角是一个蚂蚱。蚂蚱最初位于0点处,可以在直线上向正向或反向两个方向跳跃。比较特别的是,蚂蚱每次跳跃的距离比前一次跳跃多一个单位,第一次跳跃的距离为一个单位。
小B的问题是,如果让蚂蚱跳跃到x处,需要经过多少次跳跃,你能解决这个问题吗?
输入
输入中有多组测试数据。每组测试数据为单独的一行,包含一个整数x(-10^9 =< x =< 10^9)。
输出
对每组测试数据,在单独的行中输出蚂蚱最少需要跳跃的次数。
样例输入
2
6
0
样例输出
3
3
0


1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <algorithm> 6 using namespace std; 7 //n为目的站 8 int stpfunc(int n){ 9 int stp=0; 10 int idx; 11 int flag = 0; 12 int result; 13 14 for (idx = 0; idx < 32768; ++idx) { 15 int sumidx = (idx*(idx+1)/2); 16 for (stp = sumidx; stp >= -sumidx; stp -= 2) { 17 if(n == stp){ 18 result = idx; 19 flag = 1; 20 break; 21 } 22 23 } 24 if(flag == 1) break; 25 } 26 return result; 27 } 28 29 int main() 30 { 31 int n; 32 while (scanf("%d", &n) != EOF) { 33 int ret = stpfunc(n); 34 printf("%d\n",ret); 35 36 } 37 return 0; 38 }
Success time: 0 memory: 3472kb signal:0
3 3 0