Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created February 18, 2013 06:42
Show Gist options
  • Save leizongmin/4975490 to your computer and use it in GitHub Desktop.
Save leizongmin/4975490 to your computer and use it in GitHub Desktop.
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用,改用下一个数字,比如7他们用8代替,17用19代替。给定这个国家的数字,如何编程翻译成我们用的数字。
// 一个特殊的国家忌讳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));
@wfwei
Copy link

wfwei commented Feb 18, 2013

思路还好,但把问题搞反了吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment