function minNumbers() {
let len = Math.floor(Math.random() * 10 + 3);
let digit = Math.random().toString().slice(len);
console.log('给出的数字:', digit)
let arr = digit.split('');
let sort = arr.sort(function (a, b) {
return a - b;
});
if (sort[0] === '0') {
for (let i = 1; i < sort.length; i++) {
if (sort[i] !== sort[0]) {
sort[0] = sort[i];
sort[i] = '0';
break;
}
}
}
return sort.join('');
}
Created
May 10, 2018 02:40
-
-
Save xydiva/fc5d83734335ac0517caab315a4cbd6c to your computer and use it in GitHub Desktop.
给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
关于sort回调为什么返回的是
a-b
而不是a<b
或者别的表达式,请参看javascript Array.prototype.sort 排序浅谈