Interview test-C/C ++

Source: Internet
Author: User

A question searched by the people today:

Set numbers0-9Form a ring, from0Start (start point). You can go left or right to ask for a given number of times.KHow many methods can I return to the origin after the procedure? Ideas+Code

At that time, I gave the idea: because it can be left and right, and it is a circular array, we can use backtracking. Then the interviewer asked about the tracing efficiency. The answer is not ideal, but you can use pruning to reduce useless searches. Then the interviewer asked how to pruning. I thought for a moment: it seems that there are at least two possible loss cases:

1.If the number of steps is an odd number, it is impossible to return to the origin, so it can be reduced.

2.If the number of steps between the current access node and the origin is not possible, it can also be reduced.

The corresponding implementation code is as follows: the code is not strictly tested and may be incorrect orBug, Please note:

# Include <stdio. h>/** @ Param data: value of the current access node * @ Param K: current access level * @ Param N: Total number of moves * @ Param & COUNT: Number of feasible times, referenced variable */void process (INT data, int K, int N, Int & COUNT) {If (N % 2 = 1) {COUNT = 0; return ;} int min = data> (10-data )? (10-data): Data; If (min> (n-k) {return;} If (k = N) {If (Data = 0) {count ++;} return;} else {DATA = (Data + 1) % 10; process (data, k + 1, n, count ); data = (data-1) % 10; Data = (data-1) % 10; process (data, k + 1, n, count); Data = (Data + 1) % 10 ;}} int main () {int COUNT = 0; process (0, 0, 4, count); printf ("% d", 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.