"Algorithm" Joseph Ring C + + source code

Source: Internet
Author: User

#include <iostream>using namespacestd;intMain () {intA= -, b=3, winner=0;//Total number A, number to B in multiples of left, last man winner     for(intI=2; i<=a;i++) Winner= (winner+b)%i; cout<<"Winner:"<<winner+1<<Endl;}

We consider the following process

1 2 3 4 5 6 ..... K-1 k k+1 ... n-1 n

The first number for the K-brother hangs, and then the rest of the n-1 individuals, from the k+1 continue.

K+1 k+1 .... n-1 N 1 2 3 4 5 6,

The sequence numbers are all minus k, resulting in the following sequences:

1 2 3 4 5 6 ..... N-1

That is n-1 personal situation.

Assuming that the last remaining person, in the sequence of the first (n-1) person is f (n-1), then he is in the N-man sequence, numbered (K+f (n-1))%n, and has our recursive formula:

F (n) = (k+f (n-1))%n;

F (1) = 1;

http://blog.csdn.net/ice110956/article/details/12906065

"Algorithm" Joseph Ring C + + source code

Related Article

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.