zoukankan      html  css  js  c++  java
  • 杂症3

    题目:

    随机生成一个长度为 10 的整数类型的数组,例如 [2, 10, 3, 4, 5, 11, 10, 11, 20],将其排列成一个新数组,要求新数组形式如下,例如 [[2, 3, 4, 5], [10, 11], [20]]。

    解析:

    // 得到一个两数之间的随机整数,包括两个数在内
    function getRandomIntInclusive(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
    }
    // 随机生成10个整数数组, 排序, 去重
    let initArr = Array.from({ length: 10 }, (v) => { return getRandomIntInclusive(0, 99) });
    initArr.sort((a,b) => { return a - b });
    initArr = [...(new Set(initArr))];
    
    // 放入hash表
    let obj = {};
    initArr.map((i) => {
        const intNum = Math.floor(i/10);
        if (!obj[intNum]) obj[intNum] = [];
        obj[intNum].push(i);
    })
    
    // 输出结果
    const resArr = [];
    for(let i in obj) {
        resArr.push(obj[i]);
    }
    console.log(resArr);
  • 相关阅读:
    2020/4/15
    2020/4/14
    2020/4/13
    2020/4/12
    2020/4/11
    2020/4/9
    PTA录入数据库题目流程
    PTA录题
    2020/4/8
    如何把mysql workbench的数据结构和数据导出到sql表中
  • 原文地址:https://www.cnblogs.com/anbozhu7/p/11275135.html
Copyright © 2011-2022 走看看