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