题目:
用A表示第一列,用B表示第二列。。。。用Z表示第26列,用AA表示第27列。。。请写出一个函数,输入用字母表示的列号编码,输出它是第几列。
思路:
此问题最精彩之处,核心---二十六进制变十进制
function calColumn(){ // var calStr = document.getElementById("preArr").value.toUpperCase(); var calStr = 'AB'.toUpperCase()//将输入的内容都变成大写 var result = 0; for(var i = calStr.length; i > 0; i--) { var ch = calStr.charCodeAt(i-1)-64; if( i == calStr.length ){ result += ch }else{ //答疑:参考16进制 BFF=12*16^2+15*16+15 result += Math.pow(26, calStr.length-i) ;//result的结果就是列号 } } document.getElementById("showArr").value=result }
此页面下载后,直接放在浏览器演示,即可成功
git源码页面展示内容:https://github.com/Kristen-D/Algorithms/blob/master/algorithm/binaryOfNum/letterColumn.html