C 语言函数式编程练习题与解决方案156
函数式编程范式在 C 语言中可能不太常见,但通过熟练运用函数指针、高阶函数等特性,我们可以实现一些有趣的函数式编程练习题。本篇文章将提供 15 道练习题,并逐个给出详细的解决方案。
练习题1. 创建一个函数,接受一个整数数组和数组大小,并返回数组中最大的元素。
解决方案:
```c
#include
#include
int max(int *arr, int size) {
int max_element = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max_element) {
max_element = arr[i];
}
}
return max_element;
}
```
2. 创建一个函数,接受一个字符串和一个字符,并返回字符串中该字符出现的次数。
解决方案:
```c
#include
#include
int count_char(char *str, char c) {
int count = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] == c) {
count++;
}
i++;
}
return count;
}
```
3. 创建一个函数,接受一个整数数组和数组大小,并返回数组中所有偶数元素之和。
解决方案:
```c
#include
int sum_even(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
sum += arr[i];
}
}
return sum;
}
```
4. 创建一个函数,接受一个字符串,并返回字符串中所有元音字母出现的次数。
解决方案:
```c
#include
#include
int count_vowels(char *str) {
int count = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' ||
str[i] == 'o' || str[i] == 'u') {
count++;
}
i++;
}
return count;
}
```
5. 创建一个函数,接受两个字符串,并返回这两个字符串是否为回文。
解决方案:
```c
#include
#include
int is_palindrome(char *str1, char *str2) {
int len1 = strlen(str1);
int len2 = strlen(str2);
if (len1 != len2) {
return 0;
}
for (int i = 0; i < len1; i++) {
if (str1[i] != str2[len2 - 1 - i]) {
return 0;
}
}
return 1;
}
```
6. 创建一个函数,接受一个整数数组和数组大小,并返回数组中第 K 个最大的元素(K 从 1 开始)。
解决方案:
```c
#include
#include
int kth_largest(int *arr, int size, int k) {
int *sorted_arr = malloc(sizeof(int) * size);
memcpy(sorted_arr, arr, sizeof(int) * size);
qsort(sorted_arr, size, sizeof(int), compare_int);
return sorted_arr[size - k];
}
int compare_int(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
```
7. 创建一个函数,接受一个字符串,并返回字符串中单词的个数。
解决方案:
```c
#include
#include
int word_count(char *str) {
int count = 0;
int in_word = 0;
int i = 0;
while (str[i] != '\0') {
if (str[i] != ' ' && !in_word) {
in_word = 1;
count++;
} else if (str[i] == ' ') {
in_word = 0;
}
i++;
}
return count;
}
```
8. 创建一个函数,接受一个数组和数组大小,并返回数组中最长的递增子序列的长度。
解决方案:
```c
#include
#include
int longest_increasing_subsequence(int *arr, int size) {
int *lis_lengths = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
lis_lengths[i] = 1;
}
for (int i = 1; i < size; i++) {
for (int j = 0; j < i; j++) {
if (arr[i] > arr[j] && lis_lengths[i] < lis_lengths[j] + 1) {
lis_lengths[i] = lis_lengths[j] + 1;
}
}
}
int max_length = lis_lengths[0];
for (int i = 1; i < size; i++) {
if (lis_lengths[i] > max_length) {
max_length = lis_lengths[i];
}
}
free(lis_lengths);
return max_length;
}
```
9. 创建一个函数,接受一个整数数组和数组大小,并返回数组中所有子数组的和。
解决方案:
```c
#include
int *subarray_sums(int *arr, int size) {
int *sums = malloc(sizeof(int) * size * (size + 1) / 2);
int index = 0;
for (int i = 0; i < size; i++) {
int sum = 0;
for (int j = i; j < size; j++) {
sum += arr[j];
sums[index++] = sum;
}
}
return sums;
}
```
10. 创建一个函数,接受一个整数数组和数组大小,并返回数组中所有不同元素的个数。
解决方案:
```c
#include
int unique_elements(int *arr, int size) {
int count = 0;
for (int i = 0; i < size; i++) {
int unique = 1;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
unique = 0;
break;
}
}
if (unique) {
count++;
}
}
return count;
}
```
11. 创建一个函数,接受一个整数,并返回该整数的阶乘。
解决方案:
```c
#include
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
```
12. 创建一个函数,接受一个字符串,并返回字符串中所有不同字符的个数。
解决方案:
```c
#include
#include
int unique_characters(char *str) {
int count = 0;
int visited[256] = {0};
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (!visited[str[i]]) {
count++;
visited[str[i]] = 1;
}
}
return count;
}
```
13. 创建一个函数,接受一个整数数组和数组大小,并返回数组中最常出现的元素。
解决方案:
```c
#include
#include
int most_
2024-12-19
上一篇:C 语言输出打印指南
下一篇:C 语言输出选项
深入探索Java浮点数数组累加:性能优化、精度考量与实战指南
https://www.shuihudhg.cn/132918.html
C语言高效反向输出实战:多数据类型与算法详解
https://www.shuihudhg.cn/132917.html
PHP数组深度探秘:如何高效连接与操作数据库
https://www.shuihudhg.cn/132916.html
现代Java演进:从Java 8到21的关键新特性,重塑数据处理与开发范式
https://www.shuihudhg.cn/132915.html
Python数据分段提取深度解析:从基础到高级的高效策略与实践
https://www.shuihudhg.cn/132914.html
热门文章
C 语言中实现正序输出
https://www.shuihudhg.cn/2788.html
c语言选择排序算法详解
https://www.shuihudhg.cn/45804.html
C 语言函数:定义与声明
https://www.shuihudhg.cn/5703.html
C语言中的开方函数:sqrt()
https://www.shuihudhg.cn/347.html
C 语言中字符串输出的全面指南
https://www.shuihudhg.cn/4366.html