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.