百度2020校招Web前端工程师笔试卷(第一批)大题部分(21~23)

发布时间 2023-03-25 17:05:56作者: 垂序葎草

非大题在本地pdf中有详解

21.异或

题解

#include <iostream>
using namespace std;
long long change(long long x){
    if(x % 4 == 0){
        return x;
    }else if(x % 4 == 1){
        return 1;
    }else if(x % 4 == 2){
        return x + 1;
    }else if(x % 4 == 3){
        return 0; 
    }else{
        return 0;
    }
}

int main() {
    long long a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        long long ans = change(a - 1) ^ change(b);
        printf("%lld", ans);
    }
}
// 64 位输出请用 printf("%lld")

22.小度的部队

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        stack<int> stk;
        stk.push(a);
        int ans = 0;
        while(stk.size()){
            int n = stk.top();
            stk.pop();
            if(n - b <= 0){
                ans += 1;
            }
            else if((n - b) % 2 == 0){
                // /2
                int i = (n - b) / 2;
                stk.push(i);
                stk.push(i + b);
            }else if((n - b) % 2){
                ans += 1;
            }
        }
        cout << ans;
    }
}
// 64 位输出请用 printf("%lld")

23.设置标签

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <style>
       .tag-input{
    position: relative;
    border: 1px solid #cccccc;
    padding: 0 5px;
    display: flex;
    flex-flow: row wrap;
}
.js-input{
    width: 450px;
    height: 22px;
    line-height: 22px;
    font-size: 16px;
    padding: 0;
    margin: 5px 0;
    outline: none;
    border: none;
    width: 6.5em;
    height: 24px;
    line-height: 24px;
}
.tag{
    padding: 0 10px;
    margin: 5px 5px 5px 0;
    background: #25bb9b;
    color: #ffffff;
    height: 24px;
    line-height: 24px;
    border-radius: 12px;
    font-size: 13px;
}
    </style>
</head>

<body>
    <div class="tag-input">
        <span class="tag">前端</span>
        <span class="tag">编程题</span>
        <span class="tag">示例</span>
        <span class="tag">标签</span>
        <input type="text" class="js-input" maxlength="6" placeholder="请输入标签" value="123">
    </div>
<script type="text/javascript">
var tagInput = {
    isInited: false,
    init: init,
    bindEvent: bindEvent,
    addTag: addTag,
    removeTag: removeTag
};
tagInput.init();

function init() {
    var that = this;
    if (that.isInited) return;
    that.isInited = true;
    // 请修改这一行代码,保存class为js-input的输入框的dom元素引用
    // var a = document.getElementsByClassName("js-input")[0];
    // console.log("@@@",a);
    that.input = document.getElementsByClassName("js-input")[0];
    // that.input = document.querySelector('.js-input');
    that.bindEvent();
}

function bindEvent() {
    var that = this;
    var input = that.input;
    if (!input) return;
    input.addEventListener('keydown', function (event) {
        // 请修改这一行代码,判断用户是否按了回车键
        var isEnter = event.keyCode === 13;
        
        // 请修改这一行代码,判断用户是否按了删除键
        var isDelete = event.keyCode === 8;

        (isEnter || isDelete) && event.preventDefault();
        isEnter && that.addTag();
        isDelete && that.removeTag();
    });
    input.parentNode.addEventListener('click', function () {
        input.focus();
    });
}

function addTag() {
    var that = this;
    var value = that.input.value.trim();
    // console.log('@@@@',that);
    // console.log('11@@',value);
    if (!value) return;
    that.input.value = '';
        
    // 判断是否已经存在
    if (that.input.parentNode.innerText.includes(value)) return;
    var tag = document.createElement('span');
    tag.className = 'tag';
    tag.innerText = value;
    that.input.parentNode.insertBefore(tag, that.input);

}

function removeTag() {
    var that = this;
    var preTag = that.input.previousElementSibling;
    // console.log('@@@',that);
    preTag && preTag.remove();
}
</script>
</body>
</html>