Codeforces Beta Round #11 B. Jumping Jack (数学)

题目链接:http://www.codeforces.com/contest/11/problem/B

题意:给你一个目标点,初始点是0,初始跳跃距离为1,每次跳跃后跳跃距离+1,问最少几次可以跳到目标点,必须准确到达

思路:如果跳过了的话,如果跳过的距离是偶数的话,可以通过使得(now-n)/2这一步往远离终点的方向跳就好了,如果是奇数的话,那就再跳几步就好了。手动模拟下就好了

ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int a[2333];
int main()
{
    int n,i,j,d;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<0) n=-n;
        int now=0,num=1;
        int ans=0;
        while(now<n||(now-n)%2)
        {
            ans++;
            now+=num;
            num++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Codeforces Beta Round #11 B. Jumping Jack (数学))