// 指定排列组合
this._ascii = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","_","$"];
/** 数字转换64位对照表 */
private tento64(n){
var len:number = this._ascii.length;
var charVal,result="";
while (n >= 0)
{
charVal = this._ascii[n % len];//当前数&长度得到最低位
result = charVal+result;//保证相加,charVal+最低位
n = Math.floor(n / len)-1;//上一位就要除去长度
}
return result;
}
需求是:一个数组 [ a,b,c ] 作为排列组合,对应10进制数字。例如:0->a,1->b,2->c,3->aa,4->ab....以此类推。
想破了脑袋没想出来,于是就想着这个应该跟2进制差不读,然后就找规律。发现一样是2进制、8进制 那样的转换10进制的转换方法。就是采用多项式的转换方法来转换成10进制:
比如 aabc这个组合,有上面给出,数组的长度是3 ,然后多项式 就是
(a+27) + (a+9) + (b+3) + (c+0) = x
( a+a+b+c ) + ( 39 ) = x
(x就是10进制数了) (多项式中的aabc,是分别对应数组中的索引值)
逆推:
得到了多项式之后就可以逆推出由十进制转换成 X进制(字母的排列了);
(这里的3依旧是数组的长度)
先求
最低位,(x/1)%3,
最低位+1,(x/3 -1)%3,
最低位+2,(x/9 -1)%3,
最低位+2,(x/27 -1)%3,
.....
.....
.....
以此递归,可得到:
while (n >= 0)
{
charVal = this._ascii[n % len];
result = charVal+result;
n = Math.floor(n / len)-1;
}
不知道大家有什么其他更好的解释或者方法。上面的代码是借鉴了一下别人的代码。