您的位置:首页 > 其它

CF 11B Jumping Jack

2015-06-24 23:07 260 查看
首先处理正负

最优解即是 一直向右跳 正好等于x

若不能正好等于x 则要找大于x 的第一个距离 y 若(y-x)是偶数的话 则第(y-x)/2步向左跳 若为奇数 则继续向下找偶数

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int x;
while(cin>>x)
{
if(x < 0)
x = -x;
int n = 0;
while(1)
{
int dis = (n+1)*n/2;
if(dis == x)
break;
if(dis > x)
{
int k = dis - x;
if(k%2 == 0)
break;
}
n ++;
}
cout<<n<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: