之前面试被问到的一个题目,想起来了就记录下

题目大概是这样:

商店规定 3 个空瓶可以换一瓶饮料,问买 10 瓶饮料可以喝到多少瓶?

提供两种解法

1. 循环

<?php

// 循环实现
function drink($full, $empty = 0) {
    // 3 个空瓶换一瓶饮料
    $exchange = 3;
    $drink = 0;
    while ($full > 0) {
        // 喝
        $drink += $full;    // 喝过的总数增加
        $empty = $empty + $full;    // 空瓶数增加

        // 换
        $full = intval($empty / $exchange);
        $empty = $empty % $exchange;
    }


    // 考虑特殊情况,剩余空瓶数 + 1 如果可以兑换一瓶的话,就还可以跟老板预支一瓶,喝完再把空瓶给他
    if ($empty + 1 == $exchange) {
        $drink++;
    }

    return $drink;
}

var_dump(drink(10));

2. 递归

<?php

// 递归实现
function drink_recursive($full, $empty = 0, $drink = 0) {
    // 3 个空瓶换一瓶饮料
    $exchange = 3;

    // 终止条件 - 没有满瓶 && 空瓶不够兑换
    if ($full < 1 && $empty < $exchange) {
        // 考虑特殊情况,剩余空瓶数 + 1 如果可以兑换一瓶的话,就还可以跟老板预支一瓶,喝完再把空瓶给他
        if ($empty + 1 == $exchange) {
            $drink++;
        }
        return $drink;
    }

    // 空瓶兑换
    if ($empty > 0) {
        $full += intval($empty / $exchange);
        $empty = $empty % $exchange;
    }

    // 喝
    if ($full > 0) {
        $drink += $full;
        $empty += $full;
        $full = 0;
    }

    return drink_recursive($full, $empty, $drink);
}

var_dump(drink_recursive(10));

660 RMB 买了三年阿里云,三年内可以不操心续费的事情了。
用阿里云的自定义镜像功能把青岛的服务器迁到了上海,访问速度更快了。

用 deployer 发布 laravel 项目的最简配置

<?php
namespace Deployer;

require 'recipe/laravel.php';

// Set configurations
set('app_name', 'app_name');    // 应用名称
set('writable_mode', 'chown');
set('writable_use_sudo', true);
set('writable_recursive', true);

set('repository', 'ssh://[email protected]:/xxx.git');  // git 地址,要能从目标机器上访问到

// Configure servers
host('prod')
    ->hostname('host')  // 域名或者 ip
    ->user('user')  // 发布的用户名
    //->identityFile('~/.ssh/id_rsa')   // 公钥
    ->stage('production')
    ->set('deploy_path', '/data0/{{app_name}}/{{stage}}')   // 路径随便修改
    ->set('branch', 'master'); // 要发布的分支

// 加速 composer install
desc('Copy vendor directory optimized the composer install');
task('deploy:copy', function () {
    if (has('previous_release')) {
        run('cp -R {{previous_release}}/vendor {{release_path}}/vendor');
    }
});

desc('Restart php-fpm on success deploy');
task('php-fpm:restart', function () {
    // 这个命令按照实际情况修改
    run('service php7.2-fpm restart');
});

before('deploy:vendors', 'deploy:copy');
after('deploy:symlink', 'php-fpm:restart');

// 如果需要的话开启
// after('php-fpm:restart', 'artisan:horizon:terminate');