题解 | #数据分类处理#

数据分类处理

http://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

const RInput = readline().trim();

const IArr = IInput.split(' ').splice(1);
const RArr = RInput.split(' ').splice(1).sort((a, b) => a-b);

const newRarr = [...new Set(RArr)];
if (newRarr.includes('0')) {
    newRarr.splice(newRarr.indexOf('0'), 1);
}

const res = []; // 输出的结果数组
newRarr.forEach(item => {
    let hasRCount =  0;
    const hasIArrs = [];
    IArr.forEach((iItem, index) => {
        if(iItem.indexOf(item) > -1) {
            hasRCount++;
            hasIArrs.push(`${index} ${iItem}`)
        } 
    })
    if (hasRCount) {
        res.push(item);
        res.push(hasRCount);
        res.push(...hasIArrs);
    }
})
const resStr = res.join(' ');
res.unshift(resStr.split(' ').length);

print(res.join(' '))
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务