看到一道题,大家都来算算
由1-2000,从1开始隔一取数,将取得的数除去,问最后能剩余的数是几,经过几次能得到这个数。 用php怎么写出来<?php $arr = range(1,2000); $loop_for_start =0; //用于找开始的那个数 $left = 2000; //表示还剩下的数 $times =0; //循环次数 $last = 0; //最后一个 while($left>1) { $loop_for_start=0; while($arr[$loop_for_start]==0 && $loop_for_start<2000) { $loop_for_start++; } print($loop_for_start." .... "); //得到开始的位置了,开始循环 $arr[$loop_for_start]=0; //print('$arr['.$loop_for_start."]=".$arr[$loop_for_start]);///////////////////////////// $left--; while($loop_for_start<2000 && $left>1) { //得到后面一个 $loop_for_start++; while($arr[$loop_for_start]==0 && $loop_for_start<2000) { $loop_for_start++; } //再取后面一个,隔一除去 $loop_for_start++; while($arr[$loop_for_start]==0 && $loop_for_start<2000) { $loop_for_start++; } //如果不为0就可以删除了 if($arr[$loop_for_start]!=0){ $arr[$loop_for_start]=0; $left--; } } $times++; } print($times."次..\n"); //找下最后一个 $loop_for_start=0; while($arr[$loop_for_start]==0 && $loop_for_start<2000){ $loop_for_start++; } if($loop_for_start==2000){ print('失败'); } else { print('最后剩下'.$loop_for_start."\n"); } ?>