Golden October online programming Competition No. 1: Little Girl count

Source: Internet
Author: User

Golden October online programming competition first question: Little Girl count questions details:

[Golden October online programming Competition Rules]

A little girl is counting from 1 to n with her left hand fingers. She starts counting from the thumb as 1. Then, the index finger is 2, the middle finger is 3, the ring finger is 4, and the small finger is 5. Next, adjust the direction. The ring finger is counted as 6, the middle finger is 7, the index finger is 8, and the thumb is 9. Which finger will be parked at the end of the question? The numbers 1, 2, 3, 4, and 5 represent the thumb, index finger, middle finger, ring finger, and small finger in sequence.

Input Format:

Enter multiple groups of data. Each group of data occupies one row and contains only one integer N (1 <=n <= 1000000000 ).

Output Format:

Each group of data occupies one row and only contains an integer between 1 and 5, indicating the last stop finger.



Q & A description:

Input example:

1

10

1000000000

Output example:

1

2

2


Solution: Let's first observe

1 2 3 45

9 87 6

10 11 1213

17 16 15 14

18 19 20 21

25 24 23 22

...

We find that the maximum value of row N is 5 + 4 * (n-1). When n is an even number, the maximum value is at the rightmost, n is at the odd number, and the maximum value is at the leftmost, so we only need to find the given number A in the row, and then reduce it from this maximum value to a to see where it is;

#include <iostream>using namespace std;int main(){    long long n;    while (cin>>n){        double k=(n-5)*1.0/4+1;        double s=k-(int)k;        long long num=k;        if (s)            num++;        long long a=5+4*(num-1);        int answer=0;        if (a>n)            answer=a-n;        else            answer=n-a;        if (num%2==0)            answer+=1;        else            answer=5-answer;        cout<<answer<<endl;    }    return 0;}        


Golden October online programming Competition No. 1: Little Girl count

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.