在面试中,掌握C语言是许多技术岗位的基本要求。面试官往往会通过一些程序题来考察你的编程能力、逻辑思维和解决问题的能力。以下是一些常见的C语言程序题,掌握它们将帮助你更好地应对面试挑战。
1. 排序算法
排序是编程中非常基础且重要的算法之一。以下是一些常见的排序算法题目:
冒泡排序
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
快速排序
#include <stdio.h>
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]);
printf("\n");
return 0;
}
2. 字符串操作
字符串操作是C语言编程中常见的题目,以下是一些例子:
字符串反转
#include <stdio.h>
#include <string.h>
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!";
printf("Original string: %s\n", str);
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
字符串查找
#include <stdio.h>
#include <string.h>
int findSubstring(char str1[], char str2[]) {
int len1 = strlen(str1);
int len2 = strlen(str2);
for (int i = 0; i <= len1 - len2; i++) {
int j;
for (j = 0; j < len2; j++)
if (str1[i + j] != str2[j])
break;
if (j == len2)
return i;
}
return -1;
}
int main() {
char str1[] = "Hello, World!";
char str2[] = "World";
int result = findSubstring(str1, str2);
if (result != -1)
printf("Substring found at index %d\n", result);
else
printf("Substring not found\n");
return 0;
}
3. 链表操作
链表是C语言中非常重要的数据结构,以下是一些链表操作的题目:
创建单链表
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf("Created Linked list is: \n");
printList(head);
return 0;
}
链表反转
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void reverse(struct Node** head_ref) {
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
printf("Original Linked list: \n");
printList(head);
reverse(&head);
printf("Reversed Linked list: \n");
printList(head);
return 0;
}
总结
掌握这些常见的C语言程序题,将有助于你在面试中更好地展示自己的编程能力。记住,编程不仅仅是写出代码,更重要的是理解算法和数据结构,这样才能在实际工作中更好地解决问题。祝你在面试中取得好成绩!
