PHP solves the Joseph problem example based on 20 lines of associated array code, and 20 lines of Array
This article describes how PHP solves Joseph's Problem Based on 20 lines of associated array code. We will share this with you for your reference. The details are as follows:
I remember a brother who wrote about java Development some time ago told me that he had made a Joseph problem in the java 60 line, which is quite good. I think php cannot write this code.
So Haha... Over 20 lines of data, I don't believe it anymore. Haha, I don't know much about php.
Ps: In fact, I personally dislike using the number of lines of code to measure the number of lines of code. I feel that most of the lines of code are left empty. This article is for leisure and entertainment only.
Let's look back at Joseph's question: N people are in a circle. From the first one, the number of M people will be killed, and the last one will be killed. For example, N = 6, M = 5, and the order of being killed is: 5, 4, 6, 2, 3, 1.
The implementation code is as follows:
<? Php $ n = 7; echo "well, let us test $ n <br/>"; for ($ I = 1; $ I <$ n; $ I ++) {$ a ["$ I"] = $ I + 1; $ flag [$ I] = "in";} $ a ["$ n"] = 1; foreach ($ a as $ key => $ value) {echo $ key. "=> ". $ value. "</br>" ;}$ key = 1; $ out = 0; $ r = 0; while ($ out <$ n) {$ r ++; if ($ r = 2) {echo "$ a [$ key] <br/> "; // out $ a [$ key] = $ a [$ a [$ key]; $ out ++; $ r = 0 ;} $ key = $ a [$ key];}
Running result: