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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值