标签 算法 下的文章

根据分类实现无限级

数组

$array = [
    [
        "username"=>"xiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99500.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"09b42ce90879719a67f050b431b03534",
        "zongliushui"=>"500",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian2",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"99400.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"2a1ab4890db2142c6c0bb51ef77994fb",
        "zongliushui"=>"600",
        "zongyinkui"=>null
    ],
    [
        "username"=>"erjixiaxian",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"100592.80",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "userid"=>"6256d265dcc1e9cfca2c37c1093444cb",
        "zongliushui"=>"600",
        "zongyinkui"=>"592.8"
    ],
    [
        "username"=>"admin",
        "headimg"=>"/Templates/home/res/face/2.png",
        "money"=>"11038.83",
        "yongjin"=>"1.50",
        "yongjingai"=>"1",
        "jiesuan"=>"7.50",
        "agent"=>"7d9e993765d4f79748ec54cbed9a996e",
        "userid"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui"=>"5821",
        "zongyinkui"=>"8351.548"
    ],
    [
        "username"=>"xiaxian1",
        "headimg"=>"/Templates/home/res/face/3.png",
        "money"=>"97494.00",
        "yongjin"=>"0.00",
        "yongjingai"=>"0",
        "jiesuan"=>"0.00",
        "agent"=>"8e5d35bf1414296e03c7846ebab7af6e",
        "userid"=>"e89c3cb72cc95e2e7014d3d07b97beb2",
        "zongliushui"=>"3500",
        "zongyinkui"=>"-2006"
    ]
];

引用算法

//引用算法
function generateTree($array){
    //第一步 构造数据
    $items = array();
    foreach($array as $value){
        $items[$value['userid']] = $value;
    }
    //第二部 遍历数据 生成树状结构
    $tree = array();
    foreach($items as $key => $value){
        if(isset($items[$value['agent']])){
            $items[$value['agent']]['son'][] = &$items[$key];
        }else{
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}

$a = generateTree($array);
dd($a);

结果

[
    {
        "username": "admin",
        "headimg": "/Templates/home/res/face/2.png",
        "money": "11038.83",
        "yongjin": "1.50",
        "yongjingai": "1",
        "jiesuan": "7.50",
        "agent": "7d9e993765d4f79748ec54cbed9a996e",
        "userid": "8e5d35bf1414296e03c7846ebab7af6e",
        "zongliushui": "5821",
        "zongyinkui": "8351.548",
        "son": [
            {
                "username": "xiaxian2",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "99500.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "09b42ce90879719a67f050b431b03534",
                "zongliushui": "500",
                "zongyinkui": null
            },
            {
                "username": "xiaxian1",
                "headimg": "/Templates/home/res/face/3.png",
                "money": "97494.00",
                "yongjin": "0.00",
                "yongjingai": "0",
                "jiesuan": "0.00",
                "agent": "8e5d35bf1414296e03c7846ebab7af6e",
                "userid": "e89c3cb72cc95e2e7014d3d07b97beb2",
                "zongliushui": "3500",
                "zongyinkui": "-2006",
                "son": [
                    Array(),
                    Array()
                ]
            }
        ]
    }
]

阅读

百钱买百鸡

公鷄5元一隻,母鷄2元一隻,小鷄1元3只,100元可以買100只,求公鷄、母鷄、小鷄共多少隻

分析:假设有$i只鸡,$j只母鸡,$k只小鸡,并且$i+$j+$k的总数为100,即$i*5+$j*3+$k/3=100($k必须是3的倍数)

$count = 100;
function countNum($count) {
    for ($i = 1; $i < $count; $i++) {
        for ($j = 1; $j < $count; $j++) {
            for ($k = 3; $k < $count; $k = $k+3) {
                if(($i+$j+$k==$count) && ($i*5+$j*3+$k/3==$count)){
                    var_dump("公鷄:{$i}只,母鷄:{$j}只,小鷄:{$k}只");
                }
            }
        }
    }
}

简单实现无限极

<?php
function generateTree($array)
{
    //第一步 构造数据
    $items = array();
    foreach ($array as $value) {
        $items[$value['id']] = $value;
    }
    //第二步 遍历数据 生成树状结构
    $tree = array();
    foreach ($items as $key => $item) {
        if (isset($items[$item['pid']])) {
            $items[$item['pid']]['son'][] = &$items[$key];
        } else {
            $tree[] = &$items[$key];
        }
    }
    return $tree;
}
$array = [
    [
        'id' => 1,
        'pid' => 0,
    ],
    [
        'id' => 2,
        'pid' => 1,
    ],
    [
        'id' => 3,
        'pid' => 2,
    ],
    [
        'id' => 4,
        'pid' => 3,
    ],
    [
        'id' => 5,
        'pid' => 4,
    ],
    [
        'id' => 6,
        'pid' => 5,
    ],
    [
        'id' => 7,
        'pid' => 6,
    ],
    [
        'id' => 8,
        'pid' => 7,
    ],
    [
        'id' => 9,
        'pid' => 8,
    ],
    [
        'id' => 10,
        'pid' => 9,
    ],
];
$getTree = generateTree($array);
print_r($getTree);

用php实现的数字反转

示例1:

输入: 123
输出: 321

示例 2:

输入: -123
输出: -321

示例 3:

输入: 120
输出: 21

示例 4:

输入: -120
输出: -21
//实例操作
function valrev($x){
    $v = $x;
    if(substr($x,0,1)=='-'){
        $str1 = '-';
        $v = substr($v,1);
        if(substr($x,-1,1)=='0'){
            $v = rtrim($v,'0');
        }
    }else{
        $str1 = '';
        if(substr($x,-1,1)=='0'){
            $v = rtrim($v,'0');
        }
    }
    return intval($str1.strrev($v));
}

$str = -348;//must be integer
print_r(valrev($str));

根据数值获取到$array[$a]+$array[$a]=$target

$stime = microtime(true); 
$array = array(1,2,3,4,5,6,7); 
$target = 9;//得出的结论 
$array_length = count($array);//数组的长度 
 
function ch_equal($arr, $target, $stime){ 
$start_point = 0; 
$end_point = count($arr)-1; 
$len_arr = count($arr); 
$com_times = $len_arr*($len_arr-1)/2+$len_arr; 
// echo $com_times;die; 
$move_times = 0; 
while ($move_times<=$com_times && $start_point<=$end_point) { 
// echo $start_point."和".$end_point; 
if($arr[$start_point] + $arr[$end_point] == $target){ 
echo $arr[$start_point]." ".$arr[$end_point]; 
handle_time($stime); 
die; 
}; 
if($arr[$start_point] + $arr[$end_point] < $target){ 
// echo $start_point." ".$end_point."
"; 
$start_point += 1; 
$move_times += 1; 
}; 
 
if($arr[$start_point] + $arr[$end_point] > $target){ 
// echo $start_point." ".$end_point."
"; 
$end_point -= 1; 
$move_times += 1; 
}; 
} 
} 
ch_equal($array, $target, $stime); 
 
function handle_time($stime){ 
$etime=microtime(true);//获取程序执行结束的时间 
$total=$etime-$stime; //计算差值 
echo '
该脚本的执行时间为:'.$total."秒"; 
}
备案号:粤ICP备18155514号-2