Sword refers to an offer algorithm problem cycle list--Joseph question, face question 45: The last remaining digits in the circle (supplemental: Define and typedef) __ algorithm

Source: Internet
Author: User

The topics are as follows:

Analysis of the idea:
The prototype of the subject is Joseph ring problem, there are two solutions: the first method is to use the Ring chain Table simulation Circle of the classical solution, the second method is to analyze the number of deleted every time the rule and directly calculate the 1 circle of the last remaining digits. This article mainly analyzes the first method of solving problems.

Classical solution: To simulate a circle with a ring-linked list
Since there is a digital circle in the title, the natural idea is to use a ring-linked list to simulate this circle. We can create a circular list of n nodes and then delete the first m nodes in the list each time.

The idea of code implementation:
in general, you can use the std::list in the template library to simulate a ring-linked list , which is realized by:* When the current node of the list is detected as the tail point, the iterator automatically points to the position of the head node. * Since the std::list itself is not a ring structure, so whenever the iterator (Itertor) scans the end of the list, we need to move the iterator to the head of the list, which is equivalent to traversing in a circle in order.

Implemented in code, can be divided into the following modules:
1, first of all, the use of linked list containers to establish a line with the length of the list, and call the STL standard library functions, insert the list of each element.
2, according to the rules to find deleted nodes, here to pay attention, because the search process will certainly reach the end of the linked list, where the need to link a single linked list into a circular list, this article is achieved through the judgement, where the judgment needs to focus attention .
3, delete the found node, using the method is to introduce an iterator pointer, point to delete the subsequent node, and then pass to the current node.
4, pay attention to a few boundary conditions: 1 When the incoming argument does not exist 2) the list deletes only one node. The implementation code is as follows:

#include "stdafx.h" #include <list> #define FALSE 0;

#define TRUE 1;


using namespace Std; int lastremaing (unsigned int n,unsigned int m) {if n<1| |
    M&LT;1) return false;
    Establish chain list list<int> number;
    for (unsigned int i=0;i<n;i++) {number.push_back (i);
    //Find the M node List<int>::iterator renode=number.begin (); while (Number.size () >1) {for (unsigned int i=1;i<m;i++) {renode++;//First use value, then + +//Exploit
          The iterator connects the linked list to an annular list if (Renode==number.end ()) {renode=number.begin ();

    }//delete this node list<int>::iterator NextNode = + + Renode;
      if (Nextnode==number.end ()) {nextnode=number.begin ();
    }--Renode;
    Number.erase (Renode);
    Renode=nextnode;
return * (Renode); } void Test (char* testname, unsigned int n, unsigned int m, int expected) {if (testname!= NULL) printf ("%s b Egins: \ n ", Testname);
    if (lastremaing (n, m) = = expected) printf ("Solution1 passed.\n");

    else printf ("Solution1 failed.\n");
printf ("\ n");

} void Test1 () {Test ("Test1", 5, 3, 3);}


void Test2 () {Test ("Test2", 5, 2, 2);} void Test3 () {Test ("Test5", 0, 0, 0);}
    int main () {Test1 ();
    Test2 ();

    Test3 ();
    System ("pause");
return 0; }

The results of the compilation are as follows:

According to this article programming, adds the small knowledge point:
1. The difference between #include <> and #include "":
1-#include <> refers to the compiler's class library path inside the header file, regardless of your project in what directory, the compiler is a reference to the header file .
2) #include "" refers to the header file in the relative path of your program directory and invokes the header file in the project directory.
2.typedef and define what is the specific difference:
(1) #define是preprocessing instructions , in the compilation of preprocessing to make a simple replacement , do not check the correctness.
such as: #define PI 3.14.5926
In program: area=pi* R*r is automatically replaced with a value.
2 The typedef is processed at compile time , and it gives an alias to an already existing type within its scope. Common usage: Define structure, typedef struct NODE ...

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.