JavaScript题解 | #挑7#
挑7
https://www.nowcoder.com/practice/ba241b85371c409ea01ac0aa1a8d957b
const rl = require("readline").createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", (line) => { const num = parseInt(line); const res = solve(num); console.log(res); }); /** * 7的倍数 和 含 7的 * @param {*} n */ function solve(n) { let count = 0; for (let i = 1; i <= n; i++) { if (i % 7 == 0 || i.toString().split("").includes("7")) { count++; } } return count; }
直接for循环暴力解决,检查每个数是否是7的倍数(i % 7 == 0),检查是否含有7,int转string,查找是否有字符7。