今天正好在用php写项目,刚好最近在学汇编,就用php随便写个了生成任意进制N个字符(仅限单个字符),代码比较糙,还没来得急优化。回头用c++优化下再贴出来。
<?php
function createNext($str,$arr){
$tmpStr = '';
$isAdd = false;
for($i = strlen($str)-1;$i>=0;$i--){
$char = substr($str,$i,1);
if($i===strlen($str)-1){
foreach ($arr as $key => $value) {
if($value === $char){
if($key+1 < count($arr)){
$tmpStr = $arr[$key+1].$tmpStr;
}else{
$tmpStr = $arr[0].$tmpStr;
$isAdd = true;
}
}
}
}else{
if($isAdd){
foreach ($arr as $key => $value) {
if($value === $char){
if($key+1 < count($arr)){
$tmpStr = $arr[$key+1].$tmpStr;
$isAdd = false;
}else{
$tmpStr = $arr[0].$tmpStr;
$isAdd = true;
}
}
}
}else{
$tmpStr = $char.$tmpStr;
}
}
}
if($isAdd){
$tmpStr = $arr[1].$tmpStr;
}
return $tmpStr;
}
$arr = ['0','1','2','3','4','5','6','7','8','9'];
$arr = ['0','1','2','3','4','5'];
$arr = ['a','b','c','d','e'];
$res = createNext('abc',$arr);
$firstStr = $arr[0];
$total = 155;
echo $firstStr ." ";
for ($i=0; $i < $total; $i++) {
$res = createNext($firstStr,$arr);
$firstStr = $res;
echo $firstStr." ";
}
echo "\n";
?>