# 常问面试题 ## 算法 ### 微信拼手气红包算法 ```text function psq($total, $num) { $min = 0.01;//每个人最少能收到0.01元 for ($i = 1; $i < $num; $i++) { $safe_total = ($total - ($num - $i) * $min) / ($num - $i);//随机安全上限 $money = mt_rand($min * 100, $safe_total * 100) / 100; $total = $total - $money; $money_arr[] = $money; } $money_arr[] = round($total, 2); return $money_arr; } ``` ### 抽奖概率算法 ### [查找算法](/dataAtructureAlgorithm/algorithm/FIND.html) ### [排序算法](/dataAtructureAlgorithm/algorithm/SORT.html) ### 四个顶点坐标判断是否是矩形 ## php基础 ### session文件内容格式 ```text <?php session_save_path('./'); session_start(); $_SESSION['name'] = 'shiwenyuan'; $_SESSION['age'] = 100; // session file_data : name|s:10:"shiwenyuan";age|i:100; ``` ### [toString会在什么时候触发](/backend/php/basics/PHP_STR_MAGIC_METHODS.html#tostring) ### 程序运行结果 #### 题目一 ```php <?php $arr = [1, 'aa' => 2, 3,4]; foreach($arr as $key => $val) { echo ($key == 'aa' ? 5 : $val); } ``` ##### 答案 ```text 5534 ``` ## mysql ### 题目一 `table1` id|name|password ---|---|--- 1| AA | asd 2| AA | asd 3| b | asd 4| c | asd `table2` id|name ---|--- 1| AA 2| AA 3| b 4| c **select * from table1 as t1 left join table2 as t2 on t1.id = t2.id**查询结果条数,并说明原因 4条 左表优先 **select * from table1 union select * from table2**查询结果条数,并说明原因 **select * from table1 union all select * from table2**查询结果条数,并说明原因 **select id, name from table1 union select id, name from table2**查询结果条数,并说明原因 ## linux ### 统计文件字符串出现的次数 awk '{print $1}' access.log | sort |uniq -c ### 统计文件字符串出现的次数并排序 awk '{print $1}' access.log | sort |uniq -c | sort -rn ### 统计文件字符串出现的次数并排序后前100名 awk '{print $1}' access.log | sort |uniq -c | sort -rn |head -n 100