百度笔试 百度秋招 百度笔试题 1106

笔试时间:2025年11月6日

往年笔试合集:

2023春招秋招笔试合集

2024春招秋招笔试合集

第一题:坐电梯

在2077年,小A来到了一座神奇的大商场,这座大楼的地上地下都有1000层。地上从低到高的楼层为F1、F2、F3A、F5……地下从高到低为B1、B2、B3、B4……,请注意这座大楼没有F4(因楼盘开发商大楼的主人认为4不吉利),但有其他含4的楼层,如F14、F40、F401等是存在的。同时F0/B0是不存在的,F1与B1是相邻的。

更具体地,这座大楼的层号可以表示为F/B开头,后面紧接一个正整数的字符串。但有一个例外F4应该被写为F3A。

小A现在从这座大商场某一层坐电梯上升/下降,他现在想询问目的地是几层,你能帮他算一算吗?

输入描述

  • 输入的第一行包含一个数T(1≤T≤100),表示测试组数。
  • 接下来的T行每一行包含两个带空格间隔的字符串,第一个是小A出发的层号;第二个以+/-开头,之后紧跟一个正整数,表示小A上升/下降的层数。数据保证小A不会坐到不存在的楼层。
  • 输出描述

    • 对于每一组数据,输出一个层号,表示小A的目的地是哪一层。

    样例输入

    5

    F3A +1

    F5 -1

    B1 +1

    F1 -1

    B4 +7

    样例输出

    F5

    F3A

    F1

    B1

    F3A

    参考题解

    解题思路: 构建一个包含所有楼层的有序列表(地下B1000到B1,地上F1到F1000,其中F4替换为F3A),通过楼层名与索引的双向映射,将楼层移动操作转换为索引的加减运算,最终根据结果索引输出对应的楼层名称。

    C++:

    #include <iostream>
    #include <vector>
    #include <unordered_map>
    #include <string>
    using namespace std;
    
    pair<unordered_map<string, int>, unordered_map<int, string>> build_floor_mapping() {
        vector<string> floors;
        
        // 地下楼层 B1000 到 B1
        for (int i = 1000; i >= 1; i--) {
            floors.push_back("B" + to_string(i));
        }
        
        // 地上楼层 F1 到 F1000,其中F4用F3A代替
        for (int i = 1; i <= 1000; i++) {
            if (i == 4) {
                floors.push_back("F3A");
            } else {
                floors.push_back("F" + to_string(i));
            }
        }
        
        // 创建双向映射
        unordered_map<string, int> name_to_index;
        unordered_map<int, string> index_to_name;
        
        for (int idx = 0; idx < floors.size(); idx++) {
            name_to_index[floors[idx]] = idx;
            index_to_name[idx] = floors[idx];
        }
        
        return {name_to_index, index_to_name};
    }
    
    int main() {
        auto [name_to_index, index_to_name] = build_floor_mapping();
        
        int t;
        cin >> t;
        
        while (t--) {
            string current_floor, operation;
            cin >> current_floor >> operation;
            
            bool is_positive = (operation[0] == '+');
            int steps = stoi(operation.substr(1));
            if (!is_positive) {
                steps = -steps;
            }
            
            int current_index = name_to_index[current_floor];
            int target_index = current_index + steps;
            
            if (target_index < 0) {
                target_index = 0;
            } else if (target_index >= index_to_name.size()) {
                target_index = index_to_name.size() - 1;
            }
            
            cout << index_to_name[target_index] << endl;
        }
        
        return 0;
    }
    

    Java:

    import java.util.*;
    import java.io.*;
    
    public class Main {
        static Map<String, Integer> nameToIndex = new HashMap<>();
        static Map<Integer, String> indexToName = new HashMap<>();
        
        public static void buildFloorMapping() {
            List<String> floors = new ArrayList<>();
            
            // 地下楼层 B1000 到 B1
            for (int i = 1000; i >= 1; i--) {
                floors.add("B" + i);
            }
            
            // 地上楼层 F1 到 F1000,其中F4用F3A代替
            for (int i = 1; i <= 1000; i++) {
                if (i == 4) {
                    floors.add("F3A");
                } else {
                    floors.add("F" + i);
                }
            }
            
            // 创建双向映射
            for (int idx = 0; idx < floors.size(); idx++) {
                nameToIndex.put(floors.get(idx), idx);
                indexToName.put(idx, floors.get(idx));
            }
        }
        
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            buildFloorMapping();
            
            int t = Integer.parseInt(br.readLine());
            
            while (t-- > 0) {
                String[] data = br.readLine().split(" ");
                String currentFloor = data[0];
                String operation = data[1];
                
                boolean isPositive = operation.charAt(0) == '+';
                int steps = Integer.parseInt(operation.substring(1));
                if (!isPositive) {
                    steps = -steps;
                }
                
                int currentIndex = nameToIndex.get(currentFloor);
                int targetIndex = currentIndex + steps;
                
                if (targetIndex < 0) {
                    targetIndex = 0;
                } else if (targetIndex >= indexToName.size()) {
                    targetIndex = indexToName.size() - 1;
                }
                
                System.out.println(indexToName.get(targetIndex));
            }
        }
    }
    

    Python:

    def build_floor_mapping():
        floors = []
        # 地下楼层 B1000 到 B1
        for i in range(1000, 0, -1):
            floors.append(f"B{i}")
        
        # 地上楼层 F1 到 F1000,其中F4用F3A代替
        for i in range(1, 1001):
            if i == 4:
                floors.append("F3A")
            else:
                floors.append(f"F{i}")
        
        # 创建双向映射
        name_to_index = {}
        index_to_name = {}
        for idx, name in enumerate(floors):
            name_to_index[name] = idx
            index_to_name[idx] = name
        
        return name_to_index, index_to_name
    
    def main():
        # 构建楼层映射
        name_to_index, index_to_name = build_floor_mapping()
        
        # 读取测试用例数量
        t = int(input().strip())
        
        # 处理每个测试用例
        for _ in range(t):
            data = input().split()
            if l

    剩余60%内容,订阅专栏后可继续查看/也可单篇购买

    2025 春招笔试合集 文章被收录于专栏

    2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南

    全部评论

    相关推荐

    评论
    点赞
    收藏
    分享

    创作者周榜

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