# 小孩数数出列问题

PHP版

<?php

/*

* 小朋友数数算法

*/

function childrenCount(){

\$start_time = microtime(true);

\$children_list = array("zhangsan", "lisi", "wangwu", "zhaoliu", "xiaoming", "xiaohua", "laowu");

\$i = 0;

\$j = 1;

while (1){

if (\$j == 3){

\$j = 1;

unset(\$children_list[\$i]);

//            print_r(\$children_list);

}else{

\$children_list []= \$children_list[\$i];

unset(\$children_list[\$i]);

\$j++;

}

\$i++;

if (count(\$children_list) == 1){

break;

}

}

echo \$children_list[\$i], PHP_EOL;

\$end_time = microtime(true);

\$cost_time = \$end_time - \$start_time;

echo PHP_EOL, \$start_time, PHP_EOL, \$end_time, PHP_EOL, \$cost_time;

}

children_count();

?>

Golang版

package main

import "fmt"

func main()  {

var whoChild string

whoChild =childrenGame()

fmt.Println(whoChild)

}

func childrenGame()string{

var children_list = []string{"zhangsan", "lisi", "wangwu", "zhaoliu", "xiaoming", "xiaohua", "laowu"}

var j =1

for true {    //Golang是没有while循环的，可以用for true来代替

if j ==3 {

children_list =append(children_list[:0],children_list[1:]...)

j =1

}else {

children_list =append(children_list,children_list[0])

children_list =append(children_list[:0],children_list[1:]...)    //用切割切片的方式来实现对切片数据的删除

j++

}

if len(children_list) ==1 {

break

}

}

return children_list[0];

}

Java版（今天不想写了，明天更）

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

PHP版

<?php

/*

* 小朋友数数算法

*/

function childrenCount(){

\$start_time = microtime(true);

\$children_list = array("zhangsan", "lisi", "wangwu", "zhaoliu", "xiaoming", "xiaohua", "laowu");

\$i = 0;

\$j = 1;

while (1){

if (\$j == 3){

\$j = 1;

unset(\$children_list[\$i]);

//            print_r(\$children_list);

}else{

\$children_list []= \$children_list[\$i];

unset(\$children_list[\$i]);

\$j++;

}

\$i++;

if (count(\$children_list) == 1){

break;

}

}

echo \$children_list[\$i], PHP_EOL;

\$end_time = microtime(true);

\$cost_time = \$end_time - \$start_time;

echo PHP_EOL, \$start_time, PHP_EOL, \$end_time, PHP_EOL, \$cost_time;

}

children_count();

?>

Golang版

package main

import "fmt"

func main()  {

var whoChild string

whoChild =childrenGame()

fmt.Println(whoChild)

}

func childrenGame()string{

var children_list = []string{"zhangsan", "lisi", "wangwu", "zhaoliu", "xiaoming", "xiaohua", "laowu"}

var j =1

for true {    //Golang是没有while循环的，可以用for true来代替

if j ==3 {

children_list =append(children_list[:0],children_list[1:]...)

j =1

}else {

children_list =append(children_list,children_list[0])

children_list =append(children_list[:0],children_list[1:]...)    //用切割切片的方式来实现对切片数据的删除

j++

}

if len(children_list) ==1 {

break

}

}

return children_list[0];

}

Java版（今天不想写了，明天更）