N digits form a circle, and m digits are deleted.

Source: Internet
Author: User

N digits (0, 1, 2, 3... N) Form a circle, starting from the number 0. Each time the M number is deleted from the circle (the first is the number itself, and the second is the current next number) when a number is deleted,

The M number is deleted from the next one.


Find the remaining number.


# Include <iostream> # include <list> // n consecutive numbers form a circle, starting from 0. Each time the M number is deleted from this circle, after a number is deleted, the M number is deleted from the next one, and the value of the remaining number (the first is the current number and the second is the next) using namespace STD; int findlastnum (unsigned int N, unsigned int m) {If (n <1 | M <1) Return-1; unsigned int I = 0; List <int> intarrs; for (I = 0; I <n; I ++) {intarrs. push_back (I) ;}list <int >:: iterator curr = intarrs. begin (); While (intarrs. size ()> 1) {for (I = 1; I <m; I ++) {curr ++; If (curr = intarrs. end () curr = intarrs. begin () ;}list <int >:: iterator next = curr; next ++; If (next = intarrs. end () Next = intarrs. begin (); intarrs. erase (curr); curr = next;} return * curr;} int main () {int num = findlastnum (4, 3); cout <num <Endl; return 0 ;}


N digits form a circle, and m digits are deleted.

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.