来自新浪微博的面试题

使用PHP打印前一天的时间,格式如 2017-05-23 05:22:59

echo date('Y-m-d H:i:s',time()-3600*24);

以下代码会输出什么?

$a=2;
$b=&$a;
$a="2$b";
echo $a.','.$b;

22,22

$arr的值是什么,为什么?

$arr=[1,2,3];
foreach ($arr as &$v){
    //nothing to do
}
foreach ($arr as $v){
    //nothing to do
}
var_export($arr);

答案:array (0 => 1,1 => 2,2 => 2),运行foreach ($arr as &$v)

//这个过程相当于
$arr=[1,2,3];
&$v=$arr[0];//$v成为$arr[0]的引用
&$v=$arr[1];//$v成为$arr[1]的引用
&$v=$arr[2];//$v成为$arr[2]的引用
//此处结束$arr=[1,2,3];
$v=$arr[0];//因为作用域被改变的原因,此处$v实际上是$arr[2]的引用,相当于$arr[2]=$arr[0];$arr=[1,2,1];
$v=$arr[1];//相当于$arr[2]=$arr[1];$arr=[1,2,2];
$v=$arr[2];//相当于$arr[2]=$arr[2];$arr=[1,2,2];

有一群猴子玩游戏,手拉手排成一个圆环,第一轮从第一个猴子开始数,数到X个,剔出圆环,后面从剔出的这个位置开始数,还是数X个,再剔出,直到这个圆环剩下最后一个猴子为赢家,用代码描述这一过程.

答案:用递归,注意这是一个圆环,不是队列,尾巴上的数完,又从头部开始数.具体代码略.

用实际代码实现一个商品秒杀功能,要求用户不能重复下单,用户下单后有15分钟可以进行支付,到期未支付的商品回到商品池,该用户不能再次购买这件商品

答案:用redis,注意考虑临界状态,自己要实现锁.代码略.

参考资料:php的引用

本帖已被设为精华帖!
本帖由系统于 6年前 自动加精
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 13

第一题应该考的是strtotime

echo date('Y-m-d H:i:s',strtotime('-1 day'));
6年前 评论

第一题应该考的是strtotime

echo date('Y-m-d H:i:s',strtotime('-1 day'));
6年前 评论

第二题真不错。没想到。。。反正我肯定想不到这个结果。。。

6年前 评论

看样子是进不去新浪了 ?

6年前 评论

猴子的问题似乎不需要递归? :grinning:

function monkey($monkey, $x) {
    $arr = range(1, $monkey);
    $i   = 0;
    while (count($arr) > 1) {
        if (($i+1) % $x !== 0) {
            array_push($arr, $arr[$i]);
        }
        unset($arr[$i]);
        $i++;
    }
    return implode('', $arr);
}

echo 'The winner is ' . monkey(5, 5);
6年前 评论

@Kerwin 递归算的次数少了点咯

6年前 评论

新浪的题挺简单的,猴子选大王这题好多公司用啊

6年前 评论
wanghan

`$arr=
[
'赵','钱','孙','李',
'周','吴','郑','王',
'冯','陈','褚','魏',
'蒋','沈','韩','杨'
];

function countM($arr,$step)
{
$i=1;

while (count($arr)>1)
{
    if ($i==$step)
    {
        array_shift($arr);
        $i=0;
    }else
    {
        array_push($arr,array_shift($arr));
    }

    $i++;
}

echo array_shift($arr);

}`

6年前 评论
awesee

猴子的问题, 代码如下:

function king ($n, $m){
    $s = 0;
    for($i=1;$i<=$n;$i++) {
        $s = ($s+$m)%$i;
    }
    return $s+1;
}

echo king(5,2);     // 3
6年前 评论

猴子问题:
写一个环形队列,问题就迎刃而解了。

class RingQueue
{
    protected $items = [];

    public function __construct($items = [])
    {
        $this->items = array_values($items);
    }

    public function current()
    {
        return current($this->items);
    }

    public function key()
    {
        return key($this->items);
    }

    public function next()
    {
        $next = next($this->items);
        if ($next === false) {
            reset($this->items);
            return current($this->items);
        }

        return $next;
    }

    public function nextByStep($step)
    {
        $i = 0;
        for ($i; $i < $step; $i++) {
            $this->next();
        }
        return current($this->items);
    }

    public function count()
    {
        return count($this->items);
    }

    public function add($item)
    {
        $this->items[] = $itme;
        return $this;
    }

    public function remove($index)
    {
        unset($this->items[$index]);
        if (current($this->items) === false) {
            reset($this->items);
        }
        return $this;
    }

    public function print()
    {
        return implode(',', $this->items);
    }
}

function game(ringQueue $monkeys, $round)
{
    $count = $monkeys->count();

    if ($count === 1) {
        echo 'Game Over.' . PHP_EOL;
        echo 'Winner: ' . $monkeys->current() . PHP_EOL;
        return;
    }

    echo 'Round: ' . $round . PHP_EOL;
    echo 'Monkeys: ' . $monkeys->print() . PHP_EOL;
    echo 'Current Monkey: ' . $monkeys->current() . PHP_EOL;

    $step = rand(0, $count - 1);
    echo 'Step: ' . ($step + 1) . PHP_EOL;

    $outMonkey = $monkeys->nextByStep($step);

    $monkeys->remove($monkeys->key());

    echo 'Out Monkey: ' . $outMonkey . PHP_EOL;    

    echo '--------------------------' . PHP_EOL;

    game($monkeys, $round + 1);
}

$ringQueue = new ringQueue(['A', 'B', 'C', 'D']);
game($ringQueue, 1);

结果:

Round: 1
Monkeys: A,B,C,D
Current Monkey: A
Step: 2
Out Monkey: B
--------------------------
Round: 2
Monkeys: A,C,D
Current Monkey: C
Step: 1
Out Monkey: C
--------------------------
Round: 3
Monkeys: A,D
Current Monkey: D
Step: 2
Out Monkey: A
--------------------------
Game Over.
Winner: D
6年前 评论

@Kerwin Note: 如果用 array_push() 来给数组增加一个单元,还不如用 $array[] = ,因为这样没有调用函数的额外负担。

6年前 评论
class Node
{
    public $next;
    public $value;

    public function __construct($value)
    {
        $this->value = $value;
    }

    public function setNext($node)
    {
        $this->next = $node;
        return $this->next;
    }
}

function monkey($step, $node)
{
    if ($node->value == $node->next->value) return $node;
    $preNode = $node;
    for ($i = 0; $i < $step; $i++) {
        $preNode = $node;
        $node = $node->next;
    }
    $preNode->next = $node->next;
    return monkey($step, $preNode);
}

$node = new Node(1);
$next = $node->setNext(new Node(2));
$next = $next->setNext(new Node(3));
$next = $next->setNext(new Node(4));
$next = $next->setNext(new Node(5));
$next = $next->setNext(new Node(6));
$next = $next->setNext($node);

var_dump(monkey(2, $node));

欢迎指正

6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!