【JavaScript】7-16 求符合给定条件的整数集 (15分)
题目:
给定不超过6的正整数A,考虑从A开始的连续4个数字。请输出所有由它们组成的无重复数字的3位数。
输入格式:
输入在一行中给出A。
输出格式:
输出满足条件的的3位数,要求从小到大,每行6个整数。整数间以空格分隔,但行末不能有多余空格。
输入样例:
2 |
输出样例:
JavaScript代码:
const { parse } = require('path') var readline = require('readline') const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) rl.on('line', function(line) { var A = parseInt(line) var ret = gather(A) console.log(ret) }) function gather(A) { if(A<= 6 && A > 0){ var array = [A,A+1,A+2,A+3] var a2 = new Array() var a3 = new Array() var enter = new String() var arr = new Array() // var count =0 for(let i = 0,count = 0, len1 = array.length; i < len1; i++) { var a2 = array.concat(); a2.splice(i,1) for(let j = 0, len2 = a2.length; j < len2; j++) { var a3 = a2.concat(); a3.splice(j,1) for(let k = 0, len3 = a3.length; k < len3; k++) { count = count +1 //console.log(count) var s = array[i] + '' +a2[j] + '' + a3[k] if(count % 6 == 0 && count != 24) { enter = "\n" } else if(count == 24){ enter = "" } else { enter = " " } arr.push(s,enter) } } } var result = arr.join('') return result } }