Created
February 18, 2013 06:42
-
-
Save leizongmin/4975490 to your computer and use it in GitHub Desktop.
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用,改用下一个数字,比如7他们用8代替,17用19代替。给定这个国家的数字,如何编程翻译成我们用的数字。
This file contains 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
// 一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用, | |
// 改用下一个数字,比如7他们用8代替,17用19代替。 | |
// 给定这个国家的数字,如何编程翻译成我们用的数字。 | |
function convert (n) { | |
// 先转成9进制,然后将大于6的数字都加1 | |
var x = n.toString(9); | |
var r = ''; | |
for (var i = 0; i < x.length; i++) { | |
if (x[i] > 6) { | |
r += parseInt(x[i]) + 1; | |
} else { | |
r += x[i]; | |
} | |
} | |
return Number(r, 10); | |
} | |
// 测试 | |
console.log(convert(7)); | |
console.log(convert(17)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
思路还好,但把问题搞反了吧