青训营试题算法解析六

介绍

‌豆包青训营‌是由字节跳动和稀土掘金社区共同发起的技术培训和人才选拔项目。该项目的目标是培养具有职业竞争力的优秀开发工程师,并提供全程免费的课程,不收取任何费用‌。

课程内容和方向

豆包青训营的课程涵盖前端、后端和AI方向。在这个飞速发展的AI时代,学员将与豆包MarsCode团队一起深入探索技术领域,学习和运用AI,提高编程效率‌。此外,课程还包括大数据方向,适合对大数据感兴趣的学员学习‌,

本文提供训练营试题解析供参考

试题1:二进制反码转换问题

问题描述: 小C在学习二进制运算,他了解到每个非负整数都有其二进制表示。例如,整数 5 可以被表示为二进制 "101",整数 11 可以被表示为二进制 "1011",并且除了 N = 0 外,任何二进制表示中都不含前导零。

二进制的反码表示是将每个 1 变为 0,每个 0 变为 1。例如,二进制数 "101" 的二进制反码为 "010"。现在小C想知道,给定一个十进制数 N,它的二进制反码对应的十进制数是多少

java 复制代码
public class Main {
    public static int solution(int N) {
        if (N == 0) return 1;
        // 变成二进制字符串
        String result = Integer.toBinaryString(N);
        // 转换成一个个字符存进字符组
        char[] binaryArray = result.toCharArray();
        // 遍历
        for (int i = 0; i < binaryArray.length; i++) {
            // 取反
            if (binaryArray[i] == '0') {
                binaryArray[i] = '1';
            } else {
                binaryArray[i] = '0';
            }
        }
        String inverseBinary = new String(binaryArray);
        // 二进制变回十进制
        int foo = Integer.parseInt(inverseBinary, 2);
        return foo;
    }
 
    public static void main(String[] args) {
        System.out.println(solution(5) == 2 ? 1 : 0);
        System.out.println(solution(10) == 5 ? 1 : 0);
        System.out.println(solution(0) == 1 ? 1 : 0);
    }
}

试题2:充电总时间计算

问题描述:

python 复制代码
def solution(n: int, x: int, a: list) -> str:
    tot = sum(a)
    res  = tot / (4*x)
 
    return "{:.2f}".format(res)
 
if __name__ == '__main__':
    print(solution(4, 1, [2, 3, 4, 5]) == '3.50')
    print(solution(3, 2, [4, 6, 8]) == '2.25')
    print(solution(2, 1, [10, 5]) == '3.75')

试题3:判断回旋镖的存在

问题描述: 小M正在玩一个几何游戏,给定一个二维平面上的三个点 points,其中每个点用坐标 pointsi = xi, yi 表示。如果三点构成一个回旋镖,则返回 true。回旋镖的定义是三点不在一条直线上,并且这三个点互不相同。

请你帮助小M判断这些点是否构成一个回旋镖

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

bool solution(vector<vector<int>>& points) {
    // 检查点是否相同
    if (points[0] == points[1] || points[1] == points[2] || points[0] == points[2]) {
        return false;
    }

    // 计算斜率
    // 计算点1和点2的斜率
    int x1 = points[0][0], y1 = points[0][1];
    int x2 = points[1][0], y2 = points[1][1];
    int x3 = points[2][0], y3 = points[2][1];

    // 计算斜率 (y2 - y1) / (x2 - x1) 和 (y3 - y1) / (x3 - x1)
    // 为了避免除零错误,我们可以交叉相乘来比较
    if ((y2 - y1) * (x3 - x1) == (y3 - y1) * (x2 - x1)) {
        return false;
    }

    return true;
}

int main() {
    vector<vector<int>> v1 = {{1, 1}, {2, 3}, {3, 2}};
    vector<vector<int>> v2 = {{1, 1}, {2, 2}, {3, 3}};
    vector<vector<int>> v3 = {{0, 0}, {1, 1}, {1, 0}};

    cout << (solution(v1) == true) << endl;
    cout << (solution(v2) == false) << endl;
    cout << (solution(v3) == true) << endl;
    return 0;
}

试题4:判断数组是否单调

问题描述: 小S最近在研究一些数组的性质,她发现有一种非常有趣的数组被称为 单调数组。如果一个数组是单调递增或单调递减的,那么它就是单调的。

当对于所有索引i <= j时,numsi <= numsj,数组nums是单调递增的。 当对于所有索引i <= j时,numsi >= numsj,数组nums是单调递减的。 你需要编写一个程序来判断给定的数组nums是否为单调数组。如果是,返回true,否则返回false。

python 复制代码
def solution(nums: list) -> int:
    increasing = decreasing = True
    
    for i in range(1, len(nums)):
        if nums[i] > nums[i - 1]:
            decreasing = False
        elif nums[i] < nums[i - 1]:
            increasing = False
            
    return 1 if increasing or decreasing else 0
 
if __name__ == '__main__':
    print(solution(nums=[1, 2, 2, 3]) == 1)
    print(solution(nums=[6, 5, 4, 4]) == 1)
    print(solution(nums=[1, 3, 2, 4, 5]) == 0)

试题5:字符串解码问题

问题描述: 小R正在处理一个包含小写字母的字符串解码问题。给定一个长度为N的字符串S,其中包含小写英文字母。字符串的解码规则如下:

字符 'x' 应解码为 'y',字符 'y' 应解码为 'x'。 字符 'a' 应解码为 'b',字符 'b' 应解码为 'a'。 所有其他字符保持不变。 你的任务是返回解码后的字符串。

cpp 复制代码
#include <iostream>
#include <string>

std::string solution(int N, const std::string& S) {
    std::string result;  // 用于存储结果字符串

    // 遍历字符串中的每个字符
    for (char c : S) {
        // 根据字符的值进行替换
        if (c == 'x') {
            result += 'y';  // 将 'x' 替换为 'y'
        } else if (c == 'y') {
            result += 'x';  // 将 'y' 替换为 'x'
        } else if (c == 'a') {
            result += 'b';  // 将 'a' 替换为 'b'
        } else if (c == 'b') {
            result += 'a';  // 将 'b' 替换为 'a'
        } else {
            result += c;  // 其他字符保持不变
        }
    }

    return result;  // 返回结果字符串
}

int main() {
    std::cout << (solution(5, "xaytq") == "ybxtq") << std::endl;
    std::cout << (solution(6, "abcxyz") == "bacyxz") << std::endl;
    std::cout << (solution(3, "zzz") == "zzz") << std::endl;
}
相关推荐
n8n10 分钟前
Spring AI 三层架构详解:ChatModel → ChatClient → Controller,同步调用 vs 流式调用(SSE)
后端
小傅哥15 分钟前
Java + DDD,1:1 复刻 Deepseek Harness 项目
前端·后端·ai编程
Lambert28116 分钟前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
后端·aigc
积硅步致千里23 分钟前
Word 里的 .wmf 其实是 EMF:一次 metafile 转 PNG 排障
前端·后端
wabs66634 分钟前
关于二叉树【429.N叉树的层序遍历的思考】
数据结构·c++·算法·leetcode·二叉树
爱读源码的大都督35 分钟前
DeepSeek面试官问:生产RAG系统回答不准确,该如何定位和优化?这样回答,能让面试官当场给你Offer!
java·后端·python
hetao173383740 分钟前
2026-09-08 hetao1733837 的刷题记录
c++·算法
C++ 老炮儿的技术栈1 小时前
MFC CPtrArray的用法
开发语言·数据结构·c++·算法·mfc·c
杨运交1 小时前
[069][公共模块]Spring Boot 全局异常处理与参数校验实战(下):校验异常精细化处理与 WebFlux 适配
java·spring boot·后端
weixin_446260851 小时前
CABAL:用于追踪同行评审中合谋投标影响的多智能体仿真框架
人工智能·算法·机器学习