51nod 1615 跳跃的杰克

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615

题意:

思路:
一开始是觉得一旦超过了终点,中间某个地方往相反地方跳就可以了,后来发现只有超过距离是偶数时才行,奇数是不行的。那么奇数怎么办呢,继续往前跳,直到间距为偶数即可。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<stack>
 7 #include<queue>
 8 #include<cmath>
 9 #include<map>
10 #include<set>
11 using namespace std;
12 typedef long long ll;
13 typedef pair<int,int> pll;
14 const int INF = 0x3f3f3f3f;
15 const int maxn = 5000 + 5;
16 
17 ll x;
18 
19 int main()
20 {
21     //freopen("in.txt","r",stdin);
22     while(~scanf("%lld",&x))
23     {
24         if(x<0)  x=-x;
25         ll sum = 0;
26         int i=0;
27         for(;;i++)
28         {
29             sum+=i;
30             if(sum>=x)  break;
31         }
32         if(sum>x && ((sum-x)&1))
33         {
34             while((sum-x)&1) sum+=++i;
35         }
36         cout<<i<<endl;
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/zyb993963526/p/7643332.html