题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; function isSushu(val) { if(val == 1) return false; let arr = []; for(let i = 0; i<= val; i++) { val%i == 0 && (arr.push(i)); } return arr.length == 2; } void async function () { // Write your code here while(line = await readline()){ // let tokens = line.split(' '); // let a = parseInt(tokens[0]); // let b = parseInt(tokens[1]); let arr = []; // 素数对 for(let i = 1; i <= line/2; i++) { isSushu(i) && isSushu(line-i) && (arr.push({a:i, b: line - i, c: line - i - i})); } // console.log(arr); let obj = {}; let c = 1000; for(let i = 0; i< arr.length; i++){ if(arr[i].c < c) { c = arr[i].c; obj = {a: arr[i].a, b: arr[i].b}; } // arr[i].c < c && (c = arr[i].c) && (obj = {a: arr[i].a, b: arr[i].b}); } console.log(obj.a); console.log(obj.b); } }()#华为机试题#
华为机试题 文章被收录于专栏
华为机试题