题解 | #输入整型数组和排序标识,对元素升降序排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
const readline = require('readline'); const rl=readline.createInterface({ input:process.stdin, output:process.stdout }) let arr=[]; rl.on('line',(line)=>{ arr.push(line) }) rl.on('close',()=>{ let arr1=arr[0][0]; let arr2=arr[1].split(' '); let arr3=arr[2][0]; //考虑第三行输入非0,1 if(arr3==0){ arr2=arr2.sort((a,b)=>{return a-b}); } else if(arr3==1){ arr2=arr2.sort((a,b)=>{return b-a}); } console.log(arr2.join(' ')) })不难,参考HJ58