Codeforces 11B Jumping Jack (thinking)

Source: Internet
Author: User

Test instructions: from 0 on the axis can go to left and right, the distance of step I is I, to reach the minimum number of steps x (X<10^9);

Idea: Consider always to the right, if the exact arrival is the minimum number of steps;

If the distance is greater than X, and (distance-X) =n,n is an even number, it should be moved to the left N;

To move the distance N to the left, simply walk to the left of step N/2;

if (Journey-X) is odd, go straight to the even number of cases;

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intn,m,sum;intMain () {inti,j,k;  while(SCANF ("%d", &n)! =EOF) {        if(n<0) n=-N; //printf ("%d\n", n);sum=0;  for(i=0;; i++) {sum+=i; if(sum==n) Break; if(sum>n&& (sum-n)%2==0) Break; } printf ("%d\n", i); }    return 0;}

Codeforces 11B Jumping Jack (thinking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.