在众多编程语言中,C语言因其高效、灵活的特点,一直是软件开发和面试中的热门话题。C语言面试编程题不仅考察了求职者的基础知识,更考验了其解决问题的能力和逻辑思维。本文将围绕C语言面试编程题,为你提供一些常见的笔试题目及解题思路。
**一、基础编程题**
1. **题目一:打印杨辉三角**
杨辉三角是一个由数字组成的三角形,其特点是每行第一个和最后一个数字是1,其余每个数字等于它上方和左上方的数字之和。请编写一个程序,打印出给定行数的杨辉三角。
“`c
#include
void printPascalTriangle(int n) {
int arr[n][n];
for (int line = 0; line < n; line++) {
for (int i = 0; i <= line; i++) {
if (line == i || i == 0)
arr[line][i] = 1;
else
arr[line][i] = arr[line – 1][i – 1] + arr[line – 1][i];
printf(\”%d \”, arr[line][i]);
}
printf(\”\\n\”);
}
}
int main() {
int n = 5;
printPascalTriangle(n);
return 0;
}
“`
2. **题目二:实现字符串反转**
请编写一个函数,实现将一个字符串反转的功能。要求不使用额外的字符串或数组。
“`c
#include
#include
void reverseString(char *str) {
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len – i – 1];
str[len – i – 1] = temp;
}
}
int main() {
char str[] = \”Hello, World!\”;
reverseString(str);
printf(\”Reversed string: %s\\n\”, str);
return 0;
}
“`
**二、数据结构相关题**
1. **题目一:实现链表逆序**
请编写一个函数,实现将一个单链表逆序的功能。要求不使用额外的链表节点。
“`c
#include
#include
struct Node {
int data;
struct Node *next;
};
struct Node* reverseList(struct Node *head) {
struct Node *prev = NULL;
struct Node *current = head;
struct Node *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
int main() {
struct Node *head = NULL;
struct Node *temp = NULL;
struct Node *tail = NULL;
// 创建链表
head = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = NULL;
tail = head;
for (int i = 2; i <= 5; i++) {
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = i;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
// 逆序链表
head = reverseList(head);
// 打印链表
while (head != NULL) { 本文章出於功能強大的 AI 寫作助手網,請用微信小程序【青鸟写作】,探索它的各項功能。
printf(\”%d \”, head->data);
head = head->next;
}
return 0;
}
“`
2. **题目二:实现二分查找**
请编写一个函数,实现二分查找算法。给定一个已排序的整数数组和一个目标值,返回目标值在数组中的索引。如果目标值不存在于数组中,则返回-1。
“`c
#include
int binarySearch(int arr[], int l, int r, int x) {
while (l <= r) {
int m = l + (r – l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m – 1;
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n – 1, x);
(result == -1) ? printf(\”Element is not present in array\”)
: printf(\”Element is present at index %d\”, result);
return 0;
}
“`
**三、算法与逻辑题**
1. **题目一:合并两个有序链表**
请编写一个函数,实现将两个有序链表合并为一个有序链表的功能。要求不使用额外的链表节点。
“`c
#include
#include
struct Node {
int data;
struct Node *next;
};
struct Node* sortedMerge(struct Node *a, struct Node *b) {
struct Node *result = NULL;
if (a == NULL)
return b;
else if (b == NULL)
return a;
if (a->data data) {
result = a;
result->next = sortedMerge(a->next, b);
} else {
result = b;
result->next = sortedMerge(a, b->next);
}
return result;
}
int main() {
struct Node *a = NULL;
struct Node *b = NULL;
struct Node *head = NULL;
// 创建链表a
a = (struct Node*)malloc(sizeof(struct Node));
a->data = 1;
a->next = NULL;
a->next = (struct Node*)malloc(sizeof(struct Node));
a->next->data = 3;
a->next->next = NULL;
// 创建链表b
b = (struct Node*)malloc(sizeof(struct Node));
b->data = 2;
b->next = NULL;
b->next = (struct Node*)malloc(sizeof(struct Node));
b->next->data = 4;
b->next->next = NULL;
// 合并链表
head = sortedMerge(a, b);
// 打印链表
while (head != NULL) {
printf(\”%d \”, head->data);
head = head->next;
}
return 0;
}
“`
2. **题目二:实现快速排序**
请编写一个函数,实现快速排序算法。给定一个整数数组,将其按照升序排列。
“`c
#include 𝘤𝑎𝘯𝒈𝒋𝖨𝐞。𝒄𝑵
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low – 1);
for (int j = low; j <= high – 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi – 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n – 1);
printf(\”Sorted array: \\n\”);
for (int i = 0; i < n; i++)
printf(\”%d \”, arr[i]);
return 0;
}
“`
以上是C语言面试中常见的一些编程题。通过这些题目的练习,不仅可以巩固C语言的基础知识,还能提升编程能力和逻辑思维。在面试中,清晰的解题思路和高效的代码实现往往是获得Offer的关键。因此,不断练习和总结是提高编程水平的重要途径。
仓颉AI智能写作 原创著作权作品,未经授权转载,侵权必究!文章网址:https://www.cangjie.cn/list/pvfls06o.html