• 数字IC笔试千题解--编程&&脚本篇(八)


    前言

    出笔试题汇总,是为了总结秋招可能遇到的问题,做题不是目的,在做题的过程中发现自己的漏洞,巩固基础才是目的。

    所有题目结果和解释由笔者给出,答案主观性较强,若有错误欢迎评论区指出,资料整理来自于“数字IC打工人”等数字IC相关公众号,牛客网等网站真题、网络笔试真题及面经抄录。

            保持更新(2023.9.25)文章内含 单选题270道、多选题106道、填空题16道、判断题17道、简答题72道、逻辑推理题3道、C语言 python 脚本编程题8道
    在本文中笔者提供的所有代码,都写成了API,可直接copy到软件编译、运行、给出结果。  

            题目较多,即使有前人解析和强大的ChatGPT,也难免出错,若发现错误,欢迎评论区讨论。

            另外夹带一点私货~: 这一刻,我感觉一定要给.....
    数字IC笔试千题解总字数已到达15w+,网页码字卡顿情况严重,故将其分割成多个部分以方便维护,链接如下:
    数字IC笔试千题解--单选题篇(一)
    数字IC笔试千题解--单选题篇(二)
    ​​​​​​数字IC笔试千题解--多选题篇(三)​​​​​​
    数字IC笔试千题解--填空题篇(四)
    数字IC笔试千题解--判断题篇(五)
    数字IC笔试千题解--简答题篇(六)
    数字IC笔试千题解--逻辑推理篇(七)
    ​​​​​​​​​​​​​​ 数字IC笔试千题解--编程&&脚本篇(八)


    python、C语言、脚本编程题

    1. 请用C语言写一个程序,计算输入字符串中每个字母的个数(字符串只包括小写字母)。举例如输入字符串为:aaaaabbcccefff,则结果打印为:5a2b3c1e3f。(联发科2022提前批)

    答案:C语言编程题,需要记得一些C语言的基本语法,熟悉一下指针和数组,这类题目一般都不会太难。

    1. #include
    2. void main(){
    3. char str[100] = {0};
    4. int count[26] = {0};
    5. int i = 0;
    6. int count_index;
    7. scanf("%s", str);
    8. while(str[i] != '\0'){
    9. count_index = (str[i]- 'a');
    10. count[count_index] = count[count_index] + 1;
    11. i++;
    12. }
    13. for(i=0;i<26;i++){
    14. if(count[i]!=0){
    15. printf("%d%c",count[i],'a'+i);
    16. }
    17. }
    18. printf("\n");
    19. }

    输出结果:

    ​​


    2. [python]请使用种您熟悉的语言(C, Java, Python)定义二叉树的节点,并使用深度优先搜索,获取一个给定二叉树的最大深度,返回深度值。(联发科2022提前批)(同时也是Leecode easy 题)

    比如,给定二叉树[3,9,20, null,null,15,7],

    3

    / \

    9 20

    / \

    15 7

    返回它的最大深度3.

    答案:定义一个二叉树的结点结构体TreeNode,然后用递归的方式去访问二叉树的所有叶子节点,退出条件为not root,即root = None,即不存在的节点,此时not root 为真,退出该次递归函数。

    1. class TreeNode:
    2. def __init__(self, x):
    3. self.val = x
    4. self.left = None
    5. self.right = None
    6. class Solution:
    7. def maxDepth(self, root):
    8. length = 0
    9. def dfs(root, d):
    10. nonlocal length
    11. if not root:
    12. return
    13. length = max(length,d)
    14. dfs(root.left, d+1)
    15. dfs(root.right, d+1)
    16. dfs(root, 1)
    17. return length
    18. if __name__ == '__main__':
    19. # 定义题中所给的树
    20. root = TreeNode(3)
    21. root.left = TreeNode(9)
    22. node = TreeNode(20)
    23. root.right = node
    24. node.left = TreeNode(15)
    25. node.right = TreeNode(7)
    26. s = Solution() # 例化类,才能调用类中的类函数
    27. print(s.maxDepth(root))

    3. [python]给你个字符串表达式s,请使用一种您熟悉的语言(C Java, Python)实现一个基本计算器来计算并返回它的值(运算只包含+-和括号)。(联发科2022提前批)

    示例1:

    输入:s=“1+1”

    输出:2

    示例2:

    输入:s=“2-1+2”

    输出:3

    示例3:

    输入:s=“2-1+2”

    输出:3

    示例3:

    输入:s=“(1+(4+5+2)-3)+(6+8)”

    输出:23

    代码:

    使用栈存储当前括号外的结果和符号,并使用递归处理括号内的表达式。在循环中分别处理数字、加号、减号和括号。如果遇到数字,则计算当前数字;如果遇到加号或减号,则将当前数字和符号推入栈中,并将当前数字和符号重置;如果遇到左括号,则递归处理括号内的表达式,并返回括号内的计算结果和括号结束的位置;如果遇到右括号,则将当前数字和符号推入栈中,并返回括号外的计算结果以及右括号结束的位置。

    最后,我们将栈中的所有数字和符号相加,就可以得到整个表达式的计算结果。

    1. def calculate(s: str) -> int:
    2. num = 0 # 记录当前数字
    3. sign = 1 # 记录当前符号
    4. stack = [] # 用来存储当前括号外的结果和符号
    5. res = 0 # 用来记录最终结果
    6. for i in range(len(s)):
    7. if s[i].isdigit():
    8. num = num * 10 + int(s[i])
    9. elif s[i] == '+':
    10. res += num * sign
    11. num = 0
    12. sign = 1
    13. elif s[i] == '-':
    14. res += num * sign
    15. num = 0
    16. sign = -1
    17. elif s[i] == '(':
    18. stack.append(res)    # 先存结果,再存数字
    19. stack.append(sign)   
    20. res = 0
    21. sign = 1
    22. elif s[i] == ')':
    23. res += num * sign
    24. num = 0
    25. res *= stack.pop()
    26. res += stack.pop()
    27. return res + num * sign
    28. if __name__ == '__main__':
    29.     s = "(1+(4+5+2)-3)+(6+8)"
    30.     print(calculate(s))

    输出结果:23

    4. [Python]请用python写一段程序,用尽量少的语句实现下面功能:定义一个函数,判断给定目录下是否存在result.log文件,如果存在,则从result.log文件中找出所有包含fail字样(不区分大小写)的行,并将这些内容保存到一个名为fail.log的文件中,如果文件不存在则抛出异常()(联发科2022提前批)

    代码:使用了os模块中的join()函数来拼接目录路径和文件名,open()函数以只读模式打开result.log,并以写入模式打开fail.log文件,lower()函数将每行字符串中的字母转为小写字母,writelines()函数将包含fail字样的行写入fail.log文件中。程序还使用了异常处理机制来处理找不到result.log文件的情况。

    1. import os
    2. def find_fail_lines(dir_path):
    3. try:
    4. with open(os.path.join(dir_path, 'result.log'), 'r') as f1, open(os.path.join(dir_path, 'fail.log'), 'w') as f2:
    5. fail_lines = [line for line in f1 if 'fail' in line.lower()]
    6. f2.writelines(fail_lines)
    7. print('Successfully saved fail lines to fail.log')
    8. except FileNotFoundError:
    9. print('result.log file not found in the directory')
    10. # example usage
    11. find_fail_lines('E:/modelsim_project/file_handle/')

    5.请写段Python代码,打开一个文件,扫描每行,如果该匹配到“biren01”,“biren02”,……“biren99”则以行号Key,将匹配到的“birenxx”保存下来(2022壁仞)

    要写这段代码要知道,在 Python 中,可以使用 enumerate 函数来同时获取文件的行号和内容,例如:

    1. with open('file.txt') as file:
    2. for i, line in enumerate(file, 1):
    3. print(f"Line {i}: {line}",end="")

    其中变量i为行的行号,变量line存储一整行,结果展示如下:

    ​​

    代码:

    1. keywords = {} # 创建一个空字典,用于保存匹配到的关键字和行号
    2. with open('file.txt') as file:
    3. for i, line in enumerate(file, 1):
    4. for j in range(1, 100):
    5. keyword = f'biren{j:02d}' # 生成关键字,如 'biren01'、'biren02' 等
    6. if keyword in line:
    7. if i not in keywords: # 如果当前行号还没有在字典中出现过,则添加新的键值对
    8. keywords[i] = [keyword]
    9. else: # 如果当前行号已经在字典中存在,则将匹配到的关键字添加到已有的列表中
    10. keywords[i].append(keyword)
    11. for key, value in keywords.items():
    12. print(f"行号 {key},匹配到的字符为:{value}")

    第五行f'...' 表示一个格式化字符串,其中 {j:02d} 是一个占位符,表示将 j 的值格式化为两位数字,并且如果 j 不足两位,则在前面补零。这里的 d 表示将 j 格式化为十进制整数。因此,对于 j=1,生成的字符串就是 'biren01',代码输出结果如下:

    ​​


    6. 用C语言统计100~1000里面共有多少个素数,并且把所有素数打印出来。素数又称质数。所谓素数是指除了1和它本身以外,不能被任意整数整除的数.

    写一个判断是否是素数的函数,返回True or False,做个循环,对i=2到i = 根号num范围进行遍历,如果num对i取模等于零,代表i是num的因数。

    1. #include
    2. int is_prime(int num) {
    3. if (num <= 1) {
    4. return 0;
    5. }
    6. for (int i = 2; i * i <= num; i++) {
    7. if (num % i == 0) {
    8. return 0;
    9. }
    10. }
    11. return 1;
    12. }
    13. int main() {
    14. int count = 0;
    15. printf("素数有:");
    16. for (int i = 100; i <= 1000; i++) {
    17. if (is_prime(i)) {
    18. count++;
    19. }
    20. }
    21. printf("共有%d个素数\n", count);
    22. return 0;
    23. }

    7. [python]现在有两个数组a=[1,1,2,4,5],b=[1, 1,2,3,7],请用python/perl实现,找到这两个数组的交集和并集

    这里直接用python的集合变量set来做。

    1. a = [1,1,2,4,5]
    2. b = [1,1,2,3,7]
    3. intersection_A_B = set(a)-(set(a)-set(b))
    4. union_A_B = set(a).union(set(b))
    5. print(intersection_A_B)
    6. print(union_A_B)

    代码结果:

    ​​


    8. Please use any a programming language to locate the largest element in the matrix(矩阵)and print largest value,row and column of this largest value.

    Matrix: matrix [3][4]={{0,1,2,3},{8,7,6,5},{-5,9,-3,4}}

    1. #include
    2. int main(void){
    3. int matrix[3][4] = {{0,1,2,3},{8,7,6,5},{-5,9,-3,4}};
    4. int row,column,max;
    5. max = 0;
    6. for (int i =0;i<=2;i++){
    7. for(int j=0;j<=3;j++){
    8. if(matrix[i][j]>=max){
    9. row = i;
    10. column = j;
    11. max = matrix[i][j];
    12. }
    13. }
    14. }
    15. printf("max value is:%d,the row is:%d,the column is:%d\n",matrix[row][column],row+1,column+1);
    16. }

    ​​

  • 相关阅读:
    TS的安装及使用时遇到的问题
    MySQL高阶语句和视图
    Windows 下编译 TensorFlow 2.12.0 CC库
    最快的ebpf开发环境搭建方式
    SpringBoot整合Swagger2
    ArcGIS 制作科幻风格的轮廓线
    npp各个平台npp数据比较
    2023数学建模国赛选题建议及BC题思路
    Redis:报错Creating Server TCP listening socket *:6379: bind: No error
    【redis】springboot 用redis stream实现MQ消息队列 考虑异常ack重试场景
  • 原文地址:https://blog.csdn.net/qq_57502075/article/details/133262037