Created
May 26, 2014 08:53
-
-
Save sunnylost/467c0332d60457b38cbd to your computer and use it in GitHub Desktop.
practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://codeforces.com/problemset/problem/1/B | |
var min = 64; | |
function convertNumToStr(num) { | |
console.log(num) | |
if(num <= 0) throw Error('Number must be greater than 0.'); | |
var result = 27; | |
var remain; | |
var str = ''; | |
if(num > 26) { | |
while(result > 26) { | |
result = Math.floor(num / 26); | |
remain = num % 26; | |
str += String.fromCharCode(result + min); | |
} | |
return str += String.fromCharCode(remain + min); | |
} else { | |
return String.fromCharCode(num + min); | |
} | |
} | |
function convertStrToNum(str) { | |
var n = 0; | |
var len = str.length; | |
var base = 0; | |
while(len--) { | |
n += (+str[len].charCodeAt() - min) * Math.pow(26, base++); | |
} | |
return n; | |
} | |
function spreadsheets(n) { | |
var rrxcy = /R(\d+)C(\d+)/i; | |
var rcn = /([a-z]+)(\d+)/i; | |
var match; | |
if(match = n.match(rrxcy)) { | |
return convertNumToStr(match[2]) + match[1]; | |
} else { | |
match = n.match(rcn); | |
return match ? ('R' + match[2] + 'C' + convertStrToNum(match[1])) : 'Error!'; | |
} | |
} | |
//Test | |
console.log(spreadsheets('R23C55')); | |
console.log(spreadsheets('BC23')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment