Introduction to Algorithms Classic-fourth chapter 4-3 Relief payments

Source: Internet
Author: User

The question of benefits is abstracted from a few people sitting in a circle, giving each person a number, a person starting from 1, a person from the beginning of the point to K, a person, n counter-clockwise point, point to m out of a person. If we are out of the delete operation, then greatly reduce the efficiency, we will remove the person with the zero to replace, when we meet 0 o'clock not point people.

Use two methods to clockwise the person counterclockwise, if it is 0, then skip

N (n<20) individual stand in a circle, counter-clockwise numbered 1~n. There are two officials, a counter-clockwise from 1, and b clockwise from N. In each round, officials stopped for a number of K, and Officer B stopped (noting that it was possible for two officials to stop on the same person). The next person to be selected by the officials (one or 2) leaves the team.

Enter the number of the selected person in each round of the n,k,m output (if there are two people, first output is selected by a). For example, n=10,k=4,m=3, the output is 4 8, 9 5, 3 1, 2 6, 10, 7. Note: Each number of outputs should be exactly 3 columns.

Input:
10 4 3

Output:

4 8,9 5,3 6,10,7.

Note: Each number of outputs should be exactly 3 columns.

The problem is that the code to write the selection, you can do a separate function to achieve, the topic said to stand in a circle, here is a little trick to use n%b to a lap of calculation.

#include <stdio.h>#defineMAXN 22intN, K, M, A[MAXN];inti;intGointPintDintT) {//Step is D (walk clockwise to-1), Walk T-step     while(t--) {         Do{p = (p+d+n-1)%n+1;}//Important!          while(A[p] = =0);//To skip to the output of 0, go to the next non-0 number    }    returnp;}intMain () { while(SCANF ("%d%d%d", &n, &k, &m) = =3&&N) { for(i =1; I <= N; ++i) a[i]=i; intleft = n;//and the remaining number.        intP1 = N, p2 =1;  while(left) {P1= Go (P1,1, K); P2= Go (P2,-1, M); printf ("%3d", p1); Left--; if(P2! =p1) {printf ("%3d", p2); Left--; } A[P1]= A[p2] =0;//This sentence cannot be savedif(left) printf (","); } printf ("\ n"); }    return 0;

Analysis:

, each point corresponding to the number of people, which is related to our point to the first person, a certain 0 situation, so every time we point to perfect, (that is, a point of the count) to determine whether this time the point is effective, that is, the point to the right person, if the first point of people, point to 0, that is invalid, To filter down to a valid person.

In circles, because a bunch of people are sitting around the circle, so when we point to the last person N, we have to start from the first person to call, each time we have to determine whether we reach the last person, with the counter-clockwise we have to judge each time whether to reach the first end of the team.

Then do the output.

There are still deficiencies in this exercise:

For the vim of the sub-screen operation is not proficient, to be strengthened, for debugging s,n in the end it is time to execute an instruction or a number of instructions are unclear. Low commissioning efficiency.

Experience:

For a number of operations: while (n--) the number of cycles performed is n times;

When P points to the current position m, the current position should be k+m after the number of K, i.e. the K-cycles are performed.

When we order people from 1 to start the word, point to k people should be 0+k;

For the use of arrays we tend to choose a 1 larger than the topic preset in the actual operation;

Introduction to Algorithms Classic-fourth chapter 4-3 Relief payments

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.