如何创建一个将数字转换为双体形六角形的函数? [英] How to create a function that converts a Number to a Bijective Hexavigesimal?

查看:101
本文介绍了如何创建一个将数字转换为双体形六角形的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在数学方面还不够好,但是在将一个数字转换成纯字母表时,我遇到了一个问题:双体形六角形就像Microsoft Excel / OpenOffice Calc一样。



这是一个我的代码版本,但没有给我输出我需要:

 

var toHexvg = function(a){
var x =''
var let =_ abcdefghijklmnopqrstuvwxyz;
var len = let.length;
var b = a;
var cnt = 0;
var y = Array();
do {
a =(a-(a%len))/ len;
cnt ++;
} while(a!= 0)
a = b;
var vnt = 0;
do {
b + = Math.pow((len),vnt)* Math.floor(a / Math.pow((len),vnt + 1));
vnt ++;
} while(vnt!= cnt)
var c = b;
do {
y.unshift(c%len);
c =(c-(c%len))/ len;
} while(c!= 0)
for(var i in y)x + = let [y [i]];
return x;
}

我的努力最好的输出是: abcd .. 。yz ba bb bc - 虽然不是上面的实际代码。预期输出假定为 abc ... yz aa ab ac ... zz aaa aab aac ... zzzzz aaaaaa aaaaab ,你会得到照片。



基本上,我的问题更多在于做数学而不是功能。最终我的问题是:如何在Hexavigesimal转换中做数学,直到[假设]无穷大,就像Microsoft Excel。



如果可能,一个源代码,谢谢你提前。

解决方案

好的,这是我的尝试,假设你希望序列以a开头(代表0 ),然后:

  a,b,c,...,y,z,aa,ab,ac,.. 。,zy,zz,aaa,aab,... 

这个工作有希望有意义。时髦的线条是在那里,因为它在数学上更有意义的是由空字符串表示,然后a将为1等。

 code> alpha =abcdefghijklmnopqrstuvwxyz; 

函数十六进制(a){
//首先计算出有多少个数字。
a + = 1; //这行很简单
c = 0;
var x = 1;
while(a> = x){
c ++;
a - = x;
x * = 26;
}

//现在你可以进行正常的基本转换。
var s =; (var i = 0; i s = alpha.charAt(a%26)+ s;

a = Math.floor(a / 26);
}

return s;
}

但是,如果您打算按顺序打印出来,那么是更有效的方法。例如,使用递归和/或前缀和东西。


Maybe i am just not that good enough in math, but I am having a problem in converting a number into pure alphabetical Bijective Hexavigesimal just like how Microsoft Excel/OpenOffice Calc do it.

Here is a version of my code but did not give me the output i needed:


    var toHexvg = function(a){
     var x='';
     var let="_abcdefghijklmnopqrstuvwxyz";
     var len=let.length;
     var b=a;
     var cnt=0;
     var y = Array();
     do{
      a=(a-(a%len))/len;
      cnt++;
     }while(a!=0)
     a=b;
     var vnt=0;
     do{
      b+=Math.pow((len),vnt)*Math.floor(a/Math.pow((len),vnt+1));
      vnt++;
     }while(vnt!=cnt)
     var c=b;
     do{
      y.unshift( c%len );
      c=(c-(c%len))/len;
     }while(c!=0)
     for(var i in y)x+=let[y[i]];
     return x;
    }

The best output of my efforts can get is: a b c d ... y z ba bb bc - though not the actual code above. The intended output is suppose to be a b c ... y z aa ab ac ... zz aaa aab aac ... zzzzz aaaaaa aaaaab, you get the picture.

Basically, my problem is more on doing the ''math'' rather than the function. Ultimately my question is: How to do the Math in Hexavigesimal conversion, till a [supposed] infinity, just like Microsoft Excel.

And if possible, a source code, thank you in advance.

解决方案

Okay, here's my attempt, assuming you want the sequence to be start with "a" (representing 0) and going:

a, b, c, ..., y, z, aa, ab, ac, ..., zy, zz, aaa, aab, ...

This works and hopefully makes some sense. The funky line is there because it mathematically makes more sense for 0 to be represented by the empty string and then "a" would be 1, etc.

alpha = "abcdefghijklmnopqrstuvwxyz";

function hex(a) {
  // First figure out how many digits there are.
  a += 1; // This line is funky
  c = 0;
  var x = 1;      
  while (a >= x) {
    c++;
    a -= x;
    x *= 26;
  }

  // Now you can do normal base conversion.
  var s = "";
  for (var i = 0; i < c; i++) {
    s = alpha.charAt(a % 26) + s;
    a = Math.floor(a/26);
  }

  return s;
}

However, if you're planning to simply print them out in order, there are far more efficient methods. For example, using recursion and/or prefixes and stuff.

这篇关于如何创建一个将数字转换为双体形六角形的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆