Joseph Ring algorithm (C #),

Source: Internet
Author: User

Joseph Ring algorithm (C #),

A few days ago, I encountered a problem with the Joseph Ring algorithm because it was time-consuming and I had never touched on this algorithm before. Today I have time to think about this problem. I have studied it, written a piece of code, and tried it. The result should be correct. record it and try again later. The following section is taken from Baidu encyclopedia.

Joseph's ring (Joseph's problem) is a mathematical application: n people (represented by numbers 1, 2, 3... n) are known to be sitting around a round table. The number of people numbered k starts to report, and the person counting m is listed; the next person reporting the number from 1, and the person counting m is listed again; repeat this rule, until all the people around the Round Table are listed. When solving such problems, we usually change the number from 0 ~ N-1. The final [1] result + 1 is the solution of the original problem.

Paste some code below, and you can download the source code later:

Private void DoWork () {// badge of the list, used to operate the data int index = this. data. start-1; // count. When the value of Selected is calculated, a number is output and int count = 1; while (sourceList. count> 0) {// update the progress bar this. data. progressValue ++; if (count % this. data. selected = 0) {resultList. add (sourceList [index]); sourceList. remove (sourceList [index]); this. showSourecData (); this. showResultData (); count = 1 ;}else {count ++; index ++ ;}// when all data in the source data list is cyclically completed, resend and start loop again if (index = sourceList. count) index = 0; // System. threading. thread. sleep (2 );}}

Source code download

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.