A了第一道和第三道,第二道时间超了只过了20%
第一道,是八进制找7的个数
let n = parseInt(readline()); let temp; let res = 0; while (n !== 0) { temp = n % 8; n = Math.floor(n / 8); temp === 7 ? res++ : null; } console.log(res);第二道,是找最少的平方数,用动态规划,时间超了,有dalao知道怎么解答吗。。。
const n = parseInt(readline()); const dp = new Array(n + 1).fill(0); for (let i = 1; i <= n; i++) { dp[i] = i; for (let j = 1; i - j * j >= 0; j++) { dp[i] = Math.min(dp[i], dp[i - j * j] + 1); } } console.log(dp[n]);第三道,版本号排序,格式处理比较麻烦
const str = readline(); const arr = str.slice(1, str.length - 1).split(',').map(i => i.trim()); const sortFunc = (a, b) => { const arrA = a.slice(1, a.length - 1).split('.'); const arrB = b.slice(1, b.length - 1).split('.'); for (let i = 0; i < arrA.length; i++) { const numA = parseInt(arrA[i]); const numB = parseInt(arrB[i]); if (numA > numB) return 1; else if (numA < numB) return -1; } return 1; } arr.sort(sortFunc); const len = arr.length; const res = arr.map((i, index) => { if (index !== 0) return ' ' + i; return i; }) console.log('[' + res + ']');
全部评论
(2) 回帖