最大数算法
代码
<?php
function getMaxNum(array $numArr)
{
usort($numArr, 'sortFunc');
$ret = '';
foreach ($numArr as $value) {
$ret .= $value;
}
return $ret;
}
function sortFunc($one, $two)
{
if ($one == $two) {
return 0;
}
$oneLen = strlen((string)$one);
$twoLen = strlen((string)$two);
if ($oneLen == $twoLen) {
return $one > $two ? -1 : 1;
}
$subNum = $oneLen < $twoLen ? $oneLen : $twoLen;
$strOne = substr($one, 0, $subNum);
$strTwo = substr($two, 0, $subNum);
if ($strOne == $strTwo) {
$strExt = $one < $two ? substr($two, $subNum) : substr($one, $subNum);
return sortFunc(($one < $two ? $one : $strExt), ($one < $two ? $strExt : $one));
} else {
return $strOne > $strTwo ? -1 : 1;
}
}
$numArr = [3, 30, 34, 5, 9];
echo getMaxNum($numArr)."\n";
复制代码
9534330
复制代码