嵌入式笔试刷题(第20天)
✅ 11. 写一个函数,输入一个整数,返回这个整数的位数,比如输入 245,返回 3
函数原型:
int GetCount(int nValue);
✅ 解答:
int GetCount(int nValue) {
int count = 0;
if (nValue == 0) return 1;
if (nValue < 0) nValue = -nValue; // 处理负数
while (nValue) {
count++;
nValue /= 10;
}
return count;
}
思路解析:
- 每次除以 10,记录能除多少次,次数即为位数;
- 特别处理 0 和负数情况。
✅ 12. 台阶问题(最小满足条件的数)
有一条阶梯:
每次跨2阶 → 剩1阶;
跨3阶 → 剩2阶;
跨5阶 → 剩4阶;
跨6阶 → 剩5阶;
只有每次跨7阶时,才刚好走完。
函数原型:
int GetPhaseNum();
✅ 解答:
int GetPhaseNum() {
int i = 1;
while (1) {
if (i % 2 == 1 &&
i % 3 == 2 &&
i % 5 == 4 &&
i % 6 == 5 &&
i % 7 == 0) {
return i;
}
i++;
}
}
输出结果:119
解析:枚举找最小满足条件的数。
✅ 13. 已知一个链表的头节点 Node *head,写一个函数把链表逆序
✅ 解答:
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* Reverse(Node* head) {
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
while (curr) {
next = curr->next;
curr->n
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
嵌入式笔试专栏 文章被收录于专栏
本专栏系统整理了嵌入式方向笔试中常见的知识点和高频考题,涵盖基础理论、常用算法、C语言陷阱、操作系统原理、驱动开发、常见外设通信协议(如 I2C/SPI/UART)、RTOS、Linux 内核、以及实用电路知识等内容。
查看17道真题和解析