第二天华为机考练习(JavaScript node)

注:华为机试和剑指offer的JavaScript Node的书写方式不同,剑指offer只需学核心代码然后return,华为机试需要自写输入输出,输出console就行。

1. 华为机试HJ86 求最大连续bit数

https://www.nowcoder.com/questionTerminal/4b1658fd8ffb4217bc3b7e85a38cfaf2

求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1

const rl = require("readline").createInterface({ input: process.stdin });

var iter = rl[Symbol.asyncIterator]();

const readline = async () => (await iter.next()).value;

void async function () {

    // Write your code here

    while(line = await readline()){

        let num = Number(line);

        let arr=[]

        let max=0

        arr=num.toString(2).split('0')//十进制转二进制

        arr.sort((a,b)=>b-a)

        max=arr[0].length

        console.log(max);

    }

}()

2. 华为机试HJ85 最长回文子串

https://www.nowcoder.com/questionTerminal/12e081cd10ee4794a2bd70c7d68f5507

给定一个仅包含小写字母的字符串,求它的最长回文子串的长度。

所谓回文串,指左右对称的字符串。

所谓子串,指一个字符串删掉其部分前缀和后缀(也可以不删)的字符串

const rl = require("readline").createInterface({ input: process.stdin });

var iter = rl[Symbol.asyncIterator]();

const readline = async () => (await iter.next()).value;

void async function () {

    // Write your code here

    while(line = await readline()){

        let max=0

        for(let i=0;i<line.length;i++){

            for(let j=i+1;j<=line.length;j++){

                let substr=line.slice(i,j)

                let str=substr.slice(0,substr.length/2).split('').reverse().join('')

                max=substr.slice(-(substr.length/2)).includes(str)?Math.max(max,substr.length):max

            }

        }

        console.log(max);

    }

}()

3. 华为机试HJ100 等差数列

https://www.nowcoder.com/questionTerminal/f792cb014ed0474fb8f53389e7d9c07f

等差数列 2,5,8,11,14。。。。

(从 2 开始的 3 为公差的等差数列)

输出求等差数列前n项和

const rl = require("readline").createInterface({ input: process.stdin });

var iter = rl[Symbol.asyncIterator]();

const readline = async () => (await iter.next()).value;

void async function () {

    // Write your code here

    while(line = await readline()){

        let n=Number(line)

        let sum=0

        for(let i=0;i<n;i++){

            sum+=2+3*i

        }

        console.log(sum)

    }

}()

4. 华为机试HJ87 密码强度等级

https://www.nowcoder.com/questionTerminal/52d382c2a7164767bca2064c1c9d5361

密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。

一、密码长度: 5 分: 小于等于4 个字符 10 分: 5 到7 字符 25 分: 大于等于8 个字符

二、字母: 0 分: 没有字母 10 分: 密码里的字母全都是小(大)写字母 20 分: 密码里的字母符合”大小写混合“

三、数字: 0 分: 没有数字 10 分: 1 个数字 20 分: 大于1 个数字

四、符号: 0 分: 没有符号 10 分: 1 个符号 25 分: 大于1 个符号

五、奖励(只能选符合最多的那一种奖励): 2 分: 字母和数字 3 分: 字母、数字和符号 5 分: 大小写字母、数字和符号

最后的评分标准:

= 90: 非常安全 = 80: 安全(Secure) = 70: 非常强 = 60: 强(Strong) = 50: 一般(Average) = 25: 弱(Weak) = 0: 非常弱(Very_Weak)

对应输出为:

VERY_SECURE SECURE VERY_STRONG STRONG AVERAGE WEAK VERY_WEAK

请根据输入的密码字符串,进行安全评定。

const rl = require("readline").createInterface({ input: process.stdin });

var iter = rl[Symbol.asyncIterator]();

const readline = async () => (await iter.next()).value;

void async function () {

    // Write your code here

    while(line = await readline()){

        let score=0

        let count2=0

        let count3=0

        let count5=0

        //长度

        if(line.length<=4){

            score+=5

        }else if(line.length>=8){

            score+=25

        }else{

            score+=10

        }

        //字母

        if(line.match(/[a-z]/g)&&line.match(/[A-Z]/g)){

            score+=20

            count5+=2

        }else if(line.match(/[a-z]/g)||line.match(/[A-Z]/g)){

            score+=10

            count2++

            count3++

        }

        //数字

        if(line.match(/[0-9]/g)){

            count2++

            count3++

            count5++

            if(line.match(/[0-9]/g).length>1){

                score+=20

            }else{

                score+=10

            }

        }

        //符号

        if(line.match(/[^a-zA-Z0-9]/g)){

            count3++

            count5++

            if(line.match(/[^a-zA-Z0-9]/g).length>1){

                score+=25

            }else{

                score+=10

            }

        }

        //奖励

        if(count2||count3||count5){

            if(count5>count3&&count5===4){

                score+=5

            }else if(count3>count2&&count3===3){

                score+=3

            }else if(count2===2){

                score+=2

            }

        }

        //评分标准

        if(score>=90){

            console.log('VERY_SECURE')

        }else if(score>=80&&score<90){

            console.log('SECURE')

        }else if(score>=70&&score<80){

            console.log('VERY_STRONG')

        }else if(score>=60&&score<70){

            console.log('STRONG')

        }else if(score>=50&&score<60){

            console.log('AVERAGE')

        }else if(score>=25&&score<50){

            console.log('WEAK')

        }else{

             console.log('VERY_WEAK')

        }

    }

}()

每天练习算法题针对华为机考

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务