Joseph Ring Question

Source: Internet
Author: User
Title: N Numbers (0,1,..., n-1) Form a circle, starting with the number 0, each time the number of m digits is removed from the circle (the first
Is the current number itself, and the second is the next digit of the current number). When a number is deleted, the next move from the deleted number continues to delete

The number of the first M. Find the last number left in this circle

Idea: This is the Joseph ring problem, using the list in the STL to solve, is the most basic solution

int Joseph (list<int>& ring,int k) {list<int>::iterator ITR = Ring.begin (), Temp;int M;while (Ring.size () >1) {m =1;if (ITR = = Ring.end ()) ITR = Ring.begin (); while (M <k) {if (ITR = Ring.end ()) ITR =ring.begin (); Itr++;m++;if ( ITR = = Ring.end ()) ITR =ring.begin ();} temp = itr;itr++;ring.erase (temp);} ITR = Ring.begin (); return *itr;}


Joseph Ring Question

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.