"Math" Joseph Ring question, the last remaining person is the first few people

Source: Internet
Author: User

Title: Joseph Ring question, the last remaining person is the first few people. As a total of 4 people, counting to 2 of the people out, the last person left is the 1th person, return 1.

Reference: http://www.cnblogs.com/EricYang/archive/2009/09/04/1560478.html


int Josephus (int n, int k) {if (n<=0 | | k<1) throw exception (); if (n==1) return 1;//man-made rule, n is 1 o'clock return 1int res=k%2;//only two people, return Back of the person's subscript (calculated from zero) for (int i = 3; I <=n; i++) {res = (res + k)% i;} Return res+ 1;//The last value returned is calculated starting from 1}



"Math" Joseph Ring question, the last remaining person is the first few people

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.