PHP几个简单算法

生成如图所示输出:

<?php

/**
 * 按模计算
 */
{
    foreach (range(1, 100) as $n) {
        echo $n < 10 ? "0" . $n : $n, $n % 10 == 0 ? "\n" : " ";
    }
}

/**
 * 按行计算
 */
{
    foreach (range(1, 100) as $n) {

        $end = $n % 10 == 0 ? $end = "\n" : " ";

        if ($n < 10) {
            $n = "0" . $n;
        }
        echo $n, $end;
    }
}
/**
 *  连成字符串输出
 */
{
    foreach (range(1, 100) as $n) {
        $res .= $n < 10 ? "0" . $n : $n;
        $res .= $n % 10 == 0 ? "\n" : " ";
        echo $res;
        $res = '';
    }

}

a1b2c3 提取整数值(不能隐式转换)

<?php
$s = "a1b2c3d4";
echo preg_replace('/[^\d]/',"",$s);

菲波那契

<?php
function fib(int $n)
{
    [$x, $y] = [0, 1];
    for ($i = 0; $i < $n; $i++) {
        [$x, $y] = [$y, $x + $y];
        echo $x, ' ';
    }
 }
 fib(10);

记录程序运行时间

{
    // 记录开始时间
    $time = -microtime(true);
    // 这里放要执行的PHP代码,如: 
    for ($i = 0; $i < 100000; $i++) {
        print('hello'); // echo 'hello';
    }   
    $time += microtime(true);
    // 输出运行总时间
    echo "执行时间 $time seconds";
}
/**
 *
 */
{

    function getmicrotime() {
        list($usec, $sec) = explode(" ", microtime());
        return ((float) $usec + (float) $sec);
    }   

    // 记录开始时间
    $time_start = getmicrotime();
    $time = -microtime(true);

    // 这里放要执行的PHP代码,如: 
    for ($i = 0; $i < 100000; $i++) {
        // echo 'hello';
        print('hello');
    }   
    // 记录结束时间
    $time_end = getmicrotime();
    $time = $time_end - $time_start;
}

杨辉三角

/**
 * 杨辉三角
 */
function yh(int $lv = 4): array{

    $res = []; // result

    // how many level
    for ($i = 1; $i <= $lv; $i++) {

        // caculate every line
        for ($j = 1; $j <= $i; $j++) {
            $res[$i][1] = 1; // first line,first element
            if ($i == $j) {
                $res[$i][$j] = 1; // last element in line
            } else {
                $res[$i][$j] = $res[$i - 1][$j - 1] + $res[$i - 1][$j];
            }
        }

    }
    return $res;
}

$yh6 = yh(6);
var_dump($yh6);

求一个整数数组中和最大的连续子数组,例如:[4, -10, 210, -33, 45, -5, 22]的 最大连续子数组是[210, -33, 45, -5, 22](需写明思路,并编程实现)

<?php
function getMaxSubArr(array $arr) {
    $c = count($arr);
    $mckArr = []; // 放置各种可能组合(作为键名)及其和(作为值)
    for ($i = 0; $i < $c; $i++):
        $ck = ""; // 组合的键
        $subSum = 0; // 组合的和
        for ($j = $i; $j < $c; $j++){
            $ck .= $j == $i ? $j : "," . $j;
            $subSum += $arr[$j];
            $mckArr[$ck] = $subSum;
        }
    endfor;

    $mck = explode(",", array_search(max($mckArr), $mckArr)); //查找对应的和最大组合
    $res = []; // 最终结果
    foreach ($mck as $k => $v) {
        $res[] = $arr[$v]; // 把原数组中对应键的值放到结果中
    }
    return $res;
}

$arr = [4, -10, 210, -33, 45, -5, 22];
$res = getMaxSubArr($arr);
var_dump($res);

数据清洗


function actionDay() { $data = [ 0 => ['number' => '1号', "ball" => '羽毛球', "price" => '5'], 1 => ['number' => '1号', "ball" => '籃球', "price" => '10'], 2 => ['number' => '1号', "ball" => '兵兵球', "price" => '15'], 3 => ['number' => '2号', "ball" => '羽毛球', "price" => '35'], 4 => ['number' => '2号', "ball" => '籃球', "price" => '5'], 5 => ['number' => '3号', "ball" => '兵兵球', "price" => '25'], 6 => ['number' => '3号', "ball" => '兵兵球', "price" => '15'], ]; $res = []; foreach ($data as $narr) { $nk = $narr['number']; unset($narr['number']); $res[$nk][] = $narr; } return $res; } print_r(actionDay()); /** * 目标是通过程序把数组$original 重新组合成$result的样式 */ { $original = [ 0 => ['number' => '1号', "ball" => '羽毛球', "price" => '5'], 1 => ['number' => '1号', "ball" => '籃球', "price" => '10'], 2 => ['number' => '1号', "ball" => '兵兵球', "price" => '15'], 3 => ['number' => '2号', "ball" => '羽毛球', "price" => '35'], 4 => ['number' => '2号', "ball" => '籃球', "price" => '5'], 5 => ['number' => '3号', "ball" => '兵兵球', "price" => '25'], 6 => ['number' => '3号', "ball" => '兵兵球', "price" => '15'], ]; $result = [ '1号' => [ ["ball" => '羽毛球', "price" => '5'], ["ball" => '籃球', "price" => '10'], ["ball" => '兵兵球', "price" => '15'], ], '2号' => [ ["ball" => '羽毛球', "price" => '35'], ["ball" => '籃球', "price" => '5'], ], '3号' => [ ["ball" => '兵兵球', "price" => '25'], ["ball" => '兵兵球', "price" => '15'], ], ]; }

另一种排法

<?php

function actionDay() {

    $data = [
        0 => ['number' => '1号', "ball" => '羽毛球', "price" => '5'],
        1 => ['number' => '1号', "ball" => '籃球', "price" => '10'],
        2 => ['number' => '1号', "ball" => '兵兵球', "price" => '15'],
        3 => ['number' => '2号', "ball" => '羽毛球', "price" => '35'],
        4 => ['number' => '2号', "ball" => '籃球', "price" => '5'],
        5 => ['number' => '3号', "ball" => '兵兵球', "price" => '25'],
        6 => ['number' => '3号', "ball" => '兵兵球', "price" => '15'],
    ];

    $res = [];
    foreach ($data as $narr) {
        $nk = $narr['number'];
        $bk = $narr['ball'];
        $pk = $narr['price'];
        $res[$nk][] = [$bk => $pk];
    }
    return $res;
}

print_r(actionDay());

{
    $original = [
        0 => ['number' => '1号', "ball" => '羽毛球', "price" => '5'],
        1 => ['number' => '1号', "ball" => '籃球', "price" => '10'],
        2 => ['number' => '1号', "ball" => '兵兵球', "price" => '15'],
        3 => ['number' => '2号', "ball" => '羽毛球', "price" => '35'],
        4 => ['number' => '2号', "ball" => '籃球', "price" => '5'],
        5 => ['number' => '3号', "ball" => '兵兵球', "price" => '25'],
        6 => ['number' => '3号', "ball" => '籃球', "price" => '15'],
    ];
    $result = [
        '1号' => [
            '羽毛球' => '5',
            '籃球' => '10',
            '兵兵球' => '15',
        ],
        '2号' => [
            '羽毛球' => '35',
            '籃球' => '5',
        ],
        '3号' => [
            '兵兵球' => '25',
            '籃球' => '15',
        ],
    ];
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注