恒生电子嵌入式软件笔试编程题

《嵌入式软件开发笔试与面试手册》https://blog.nowcoder.net/zhuanlan/jvN8gj

《软件开发笔试汇总》https://blog.nowcoder.net/zhuanlan/0oDWVm

核心代码模式

第一题

给出一个正整数,请写一个算法来判断该数是质数还是合数,如果是合数,需输出该合数的质数因子。说明:直接使用开发语言提供的函数不得分。

判断:8 13 99 177 8888是质数还是合数

 

输入描述

8 13 99 177 8888

输出描述

质数或者合数

示例 1

输入:

100

输出:

[100]是合数.

100=2*2*5*5

说明:

整数100是合数,拆分后的质数因子为:2*2*5*5

#include <iostream>
#include <vector>
using namespace std;

// 函数用于判断数字是否是质数,并返回它的质数因子(如果有的话)
pair<bool, vector<int>> isPrimeAndFactors(int n) {
    if (n <= 1) return {false, {}};
    vector<int> factors;
    for (int i = 2; i*i <= n; i++) {
        while (n % i == 0) {
            factors.push_back(i);
            n /= i;
        }
    }
    if (n > 1) factors.push_back(n); // 如果n不是1,那么n是最后的质数因子
    return {factors.size() == 1 && factors[0] == n, factors};
}

int main() {
    int n;
    cin >> n;

    auto [isPrime, factors] = isPrimeAndFactors(n);
    if (isPrime) {
        cout << n << " 是质数." << endl;
    } else {
        cout << n << " 是合数." << endl;
        cout << n << " = ";
        for (size_t i = 0; i < factors.size(); i++) {
            cout << factors[i];
            if (i < factors.size() - 1) cout << "*";
        }
        cout << endl;
    }
    return 0;
}

第二题

项目团队需要实现一个路由转发模块,转发模块的核心功能之一是根据功能号/接口名来进行路由转发。路由匹配的基本原理是判断实际请求中的功能号/接口名是否与预先配置的转发规则匹配,转发规则支持 '?' '*' 通配符( *'?'表示匹配任意单个字符;' '表示匹配零个或多个任意字符)。比如转发规则串配置为"600???",当实际请求中的功能号是"600570”时就判定为匹配。现在请你编码实现一个函数来支持是否匹配的判断。

注意:要自己编码实现逻辑,不可以调用已有的实现函数。另外在函数编码之前可以用注释说明自己的逻辑思路。

 

示例 1

输入:

aa

a

输出:

false

说明:

"a"无法匹配"aa"整个字符串

#include <iostream>
#include <string>
using namespace std;

bool match(const string& pattern, const string& target, int pIndex = 0, int tIndex = 0) {
    if (pIndex == pattern.size() && tIndex == target.size()) return true; 
    if (pIndex == pattern.size()) return false;

    if (pattern[pIndex] == '*') {
        for (int i = tIndex; i <= target.size(); ++i) {
            if (match(pattern, target, pIndex + 1, i)) return true;
        }
    } else if (tIndex < target.size() && (pattern[pIndex] == '?' || pattern[pIndex] == target[tIndex])) {
        return match(pattern, target, pIndex + 1, tIndex + 1);
    }

    return false;
}

int main() {
    string pattern, target;
    cin >> pattern;
    cin >> target;

    cout << boolalpha << match(pattern, target) << endl;

    return 0;
}

#恒生电子##嵌入式##软件开发##笔试#

本专栏主要发布2024年嵌入式软件开发相关岗位笔试真题(嵌入式软件开发、通用软件开发、C/C++软件开发、算法工程师、测试开发等)主要是算法编程题,其中一些岗位笔试含有对应的选择题、填空题、简单题。

全部评论
mark
点赞 回复
分享
发布于 04-21 11:48 广东
感谢分享
点赞 回复
分享
发布于 04-22 19:14 广东
联想
校招火热招聘中
官网直投

相关推荐

2 3 评论
分享
牛客网
牛客企业服务