Interview question: Give a rule to let the input number retrieve the finger

Source: Internet
Author: User

The question is similar: (because it was given orally, I understood it for a while)

There are the following rules: (the original question is that a series corresponds to five fingers)

X Y

0 1

1 2

2 3

3 4

4 5

5 4

6 3

7 2

8 1

9 2

10 3

11 4

12 5

13 4

14 3

......

 

Input an X at will and output a corresponding y

 

After analysis, I use the following method:

Find the rule Y every 8 groups, so we can make X to 8 get the remainder, and the remainder will be related to Y, as shown below:

Int Gety (const int X)

{

Int temp = x % 8; // 0, 1, 2, 3, 4, 5, 6, 7

If (temp> 4)

Return 8-Temp + 1;

Else

Return temp + 1;

}

 

Although this can be achieved, the Examiner requiresNo greater than judgmentI have been thinking about it for a long time and have not come up with a more sophisticated approach than "use". It is really depressing. Please kindly advise!

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.