PHP solves the Joseph problem example based on 20 lines of associated array code, and 20 lines of Array

Source: Internet
Author: User

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:

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.