:::tips 习题很多都是重复之前的题目,要用函数实现而已,虽然显得多此一举,题目就是题目,没有凸显函数设计的目的。 :::

1、写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。

解题思路:使用辗转相除法求出 gcd ,根据公式 课后习题 - 图3得出 lcm 。答案中无非用全局变量替代函数传参实现,两者都是一致的,减少使用全局变量进行传递是一个很好的习惯。

  1. #include <stdio.h>
  2. int gcd(int a, int b) {
  3. while (b != 0) {
  4. int tmp = a;
  5. a = b;
  6. b = tmp % b;
  7. }
  8. return a;
  9. }
  10. int lcm(int a, int b, int _gcd) {
  11. return a * b / _gcd;
  12. }
  13. int main () {
  14. printf("Please input two number:");
  15. int a, b;
  16. scanf("%d %d", &a, &b);
  17. int _gcd = gcd(a, b);
  18. printf("gcd: %d\n", _gcd);
  19. printf("lcm: %d\n", lcm(a, b, _gcd));
  20. return 0;
  21. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/gcdLcm.c -o ./bin/gcdLcm
b12@PC:~/chapter7$ ./bin/gcdLcm
Please input two number:24 16
gcd: 8
lcm: 48 :::

2、求方程 课后习题 - 图4 的根,用 3 个函数分别三种情况下的根并输出结果,从主函数输入 a,b,c 的值。

课后习题 - 图5
解题思路:关键就是判断 课后习题 - 图60 的关系,然后进行函数调用(参考答案好牵强的函数调用)。需要注意的就是分母一定要用括号括起来 (2 * a)

  1. #include <stdio.h>
  2. #include <math.h>
  3. double x1, x2, p, q, delta;
  4. void gt(double a, double b) {
  5. double disc = sqrt(delta);
  6. x1 = (-b + disc) / (2 * a);
  7. x2 = (-b - disc) / (2 * a);
  8. }
  9. void eq(double a, double b) {
  10. x1 = x2 = -b / (2 * a);
  11. }
  12. void lt(double a, double b) {
  13. p = -b / (2 * a);
  14. q = sqrt(-delta) / (2 * a);
  15. }
  16. int main () {
  17. printf("Please input three numbers:");
  18. double a, b, c;
  19. scanf("%lf %lf %lf", &a, &b, &c);
  20. delta = b * b - 4 * a * c;
  21. if (delta > 0) {
  22. gt(a, b);
  23. printf("x1=%f x2=%f\n", x1, x2);
  24. } else if (delta < 0) {
  25. lt(a, b);
  26. printf("x1=%f+%fi x2=%f-%fi\n", p, q, p, q);
  27. } else {
  28. eq(a, b);
  29. printf("x1=x2=%f\n", x1);
  30. }
  31. return 0;
  32. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/sqrt.c -o ./bin/sqrt -lm
b12@PC:~/chapter7$ ./bin/sqrt
Please input three numbers:2 4 1
x1=-0.292893 x2=-1.707107
b12@PC:~/chapter7$ ./bin/sqrt
Please input three numbers:1 2 1
x1=x2=-1.000000
b12@PC:~/chapter7$ ./bin/sqrt
Please input three numbers:2 4 3
x1=-1.000000+0.707107i x2=-1.000000-0.707107i :::

3、写一个判素数的函数,在主函数输入一个整数,输出是否为素数的信息。

解题思路:判断素数方法如下论证,不再累述为什么答案可以使用 课后习题 - 图7 的原因。
课后习题
然后这里再使用我们之前手动实现的 sqrt 函数。
69.x的平方根(简单)

  1. #include <stdio.h>
  2. #include <math.h> // fabs
  3. double mySqrt(int x){
  4. if (x == 0) return 0;
  5. // 利用牛顿迭代法进行
  6. double C = x, X0 = x;
  7. while (1) {
  8. double X1 = 0.5 * (X0 + C / X0);
  9. if (fabs(X0 - X1) < 1e-7) {
  10. break;
  11. }
  12. X0 = X1;
  13. }
  14. return X0;
  15. }
  16. int prime(int a) {
  17. int disc = (int)mySqrt(a);
  18. for (int i = 2; i <= disc; i++) {
  19. if (0 == a % i) {
  20. return 0;
  21. }
  22. }
  23. return a != 1; // 1不是质数
  24. }
  25. int main () {
  26. printf("Please input a number:");
  27. int a;
  28. scanf("%d", &a);
  29. if (prime(a)) {
  30. printf("%d is a prime\n", a);
  31. } else {
  32. printf("%d is not a prime\n", a);
  33. }
  34. return 0;
  35. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/prime.c -o ./bin/prime
b12@PC:~/chapter7$ ./bin/prime
Please input a number:1
1 is not a prime
b12@PC:~/chapter7$ ./bin/prime
Please input a number:17
17 is a prime
b12@PC:~/chapter7$ ./bin/prime
Please input a number:25
25 is not a prime :::

4、写一个函数,使给定的一个 3×3 的二维整型数组转置,即行列互换。

解题思路:题目的转置是由 课后习题 - 图8的方式而不是将矩阵旋转多少度(这种更难实现),即数字按主对角线对换(左下角和右上角对换即可),因此每行交换次数为 课后习题 - 图9,这里减一是主对角线自身不需要对换。

  1. #include <stdio.h>
  2. #define N 3
  3. void convert(int array[][3]) {
  4. for (int row = 0; row < N; row++) {
  5. // 第 row 行需要按对称轴旋转 N-1-row 个数字
  6. for (int col = row + 1; col < N; col++) {
  7. int tmp = array[row][col];
  8. array[row][col] = array[col][row];
  9. array[col][row] = tmp;
  10. }
  11. }
  12. }
  13. int main () {
  14. int matrix[N][N];
  15. printf("Please input %d numbers:", N * N);
  16. for (int i = 0; i < N; i++) {
  17. for (int j = 0; j < N; j++) {
  18. scanf("%d", &matrix[i][j]);
  19. }
  20. }
  21. printf("Original matrix:\n");
  22. for (int i = 0; i < N; i++) {
  23. for (int j = 0; j < N; j++) {
  24. printf("%-5d", matrix[i][j]);
  25. }
  26. printf("\n");
  27. }
  28. printf("Convert matrix:\n");
  29. for (int i = 0; i < N; i++) {
  30. for (int j = 0; j < N; j++) {
  31. printf("%-5d", matrix[i][j]);
  32. }
  33. printf("\n");
  34. }
  35. return 0;
  36. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/convert.c -o ./bin/convert
b12@PC:~/chapter7$ ./bin/convert
Please input 9 numbers:1 2 3 4 5
6 7 8 9 (tips:注意这里输入为什么可以换行,原因就是%d)
Original matrix:
1 2 3
4 5 6
7 8 9
Convert matrix:
1 2 3
4 5 6
7 8 9 :::

5、写一个函数,使输入的一个字符串按反序存放,在主函数中输入和输出字符串。

解题思路:字符数组和数组类似,原理一样。
课后习题
额外空间倒置填充递归实现的方式:
PTA编程—数组

6、写一个函数,将两个字符串连接。

解题思路:本质上让你实现标准库函数: strcat(char *dest, char *src) ,出现在历年考研题目中。

  1. 实现三个数组,其中第三个长度一定要大与前两者之长。
  2. IO 两个字符串然后进行连接操作。 :::tips 如果不对数组进行初始化,那么一定要在最后拷贝的时候手动添加一个 '\0' 才可以。 ::: ```c

    include

    define MAXSIZE 100

void concatenate(char str1, char str2, char *str3) { int idx = 0; for (int i = 0; str1[i] != ‘\0’; i++) { str3[idx++] = str1[i]; } for (int i = 0; str2[i] != ‘\0’; i++) { str3[idx++] = str2[i]; } str3[idx] = ‘\0’; // 如果数组进行初始化过且有效长度内则不变 }

int main () { char str1[MAXSIZE], str2[MAXSIZE], str3[MAXSIZE]; printf(“Please input str1:”); scanf(“%[^\n]s”, str1); getchar(); // 由于使用非标准的输入法,因此必须添加吸收\n printf(“Please input str2:”); scanf(“%[^\n]s”, str2); getchar(); concatenate(str1, str2, str3); printf(“str1 + str2 = %s\n”, str3); return 0; }

  1. 编译运行:
  2. :::success
  3. b12@PC:~/chapter7$ gcc -Wall ./src/strcat.c -o ./bin/strcat<br />b12@PC:~/chapter7$ ./bin/strcat<br />Please input str1:You are<br />Please input str2: the best one!<br />str1 + str2 = You are the best one!
  4. :::
  5. 题目中的 `scanf("%s")` 是无法应对含有空格的字符串的。或者使用 `gets(str1);` 虽然编译收到警告,建议使用 `char *fgets(char *buf, int bufsize, FILE *stream);` 函数,然而当输入不足 `n` 字符前就遇到换行,该 `\n` 会被加入到 `buffer` 内;如果输入超过 `n` ,那么只会截取前 `n-1` 个字符。即是说不管你怎么输入,此函数永远预留一个 `'\0'` `buffer` 数组。最大的麻烦就是输入不足 `n` 个任然将 `\n` 加入是无法**忍受**的。<br />例如将上述换成 `fgets(str1, MAXSIZE, stdin);` 进行编译运行,就会有 `\n`
  6. :::success
  7. b12@PC:~/chapter7$ gcc -Wall ./src/strcat.c -o ./bin/strcat<br />b12@PC:~/chapter7$ ./bin/strcat<br />Please input str1:You are<br />Please input str2: newline!<br />str1 + str2 = You are<br /> newline!
  8. b12@PC:~/chapter7$
  9. :::
  10. 解决办法就是判断字符串长度,然后在最后位置将 `\n` 删除掉。
  11. ```c
  12. #include <stdio.h>
  13. #include <string.h>
  14. #define MAXSIZE 100
  15. void concatenate(char *str1, char *str2, char *str3) {
  16. int idx = 0;
  17. for (int i = 0; str1[i] != '\0'; i++) {
  18. str3[idx++] = str1[i];
  19. }
  20. for (int i = 0; str2[i] != '\0'; i++) {
  21. str3[idx++] = str2[i];
  22. }
  23. str3[idx] = '\0'; // 如果数组进行初始化过且有效长度内则不变
  24. }
  25. int main () {
  26. char str1[MAXSIZE], str2[MAXSIZE], str3[MAXSIZE];
  27. printf("Please input str1:");
  28. fgets(str1, MAXSIZE, stdin);
  29. str1[strlen(str1)-1] = '\0';
  30. printf("Please input str2:");
  31. fgets(str2, MAXSIZE, stdin);
  32. str2[strlen(str2)-1] = '\0';
  33. concatenate(str1, str2, str3);
  34. printf("str1 + str2 = %s\n", str3);
  35. return 0;
  36. }

:::success b12@PC:~/chapter7$ gcc -Wall ./src/strcat.c -o ./bin/strcat
b12@PC:~/chapter7$ ./bin/strcat
Please input str1:You are
Please input str2: the newline!
str1 + str2 = You are the newline! :::

7、写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出。

解题思路:元音字母大小写判断即可,复制字符串后需要留意字符数组中是否进行初始化,没有进行初始化必定要手动在最后加上 '\0' ,否则非法访问。 :::tips 为什么作者这里要使用 gets() 函数而不是 scanf ,任然是输入可能存在空格的字符串。而使用 fgets 在输入不满足 N 字节长之前遇到 \n 不会像 gets() 一样将 '\n' 从缓冲区扔出去,而是直接放入 buffer 内。 :::

  1. #include <stdio.h>
  2. #define N 100
  3. void filterVowels(char dest[], char src[]) {
  4. int idx = 0;
  5. for (int i = 0; '\0' != src[i]; i++) {
  6. if ('a' == src[i] || 'A' == src[i] ||
  7. 'e' == src[i] || 'E' == src[i] ||
  8. 'i' == src[i] || 'I' == src[i] ||
  9. 'o' == src[i] || 'O' == src[i] ||
  10. 'u' == src[i] || 'U' == src[i]) {
  11. dest[idx++] = src[i];
  12. }
  13. }
  14. dest[idx] = '\0';
  15. }
  16. int main () {
  17. char str1[N], str2[N];
  18. printf("Please input string:");
  19. fgets(str1, N, stdin); // 注意会将\n写入str1
  20. filterVowels(str2, str1);
  21. printf("The vowel letters are %s\n", str2);
  22. return 0;
  23. }

编译运行:(注意下面有空格的输入字符串) :::success b12@PC:~/chapter7$ gcc -Wall ./src/filterVowels.c -o ./bin/filterVowels
b12@PC:~/chapter7$ ./bin/filterVowels
Please input string:abcAe abcAeIj
The vowel letters are aAeaAeI :::

8、写一个函数,输入一个4位数字,要求输出这4个数字字符,但每两个数字间空一个空格。如输入1990,应输出“1 9 9 0”

解题思路:注意为什么要从最后开始向前进行操作,如果从左向右必定导致后面的数据被覆盖,而数组足够长从后往前一定不会覆盖前者。对于原数组后 课后习题 - 图10 个字符(不包含 \0 ),全部转换为 x 前缀空格+字符共需要 课后习题 - 图11长度,而第一个字符原地不动。 :::tips 参考答案中先将最后的 \0 移动到对应的位置,然后前面加上了一个空格,很明显是多余的,不满足两数字之间有一个空格。 :::

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 100
  4. void spaceJoin(char s[]) {
  5. for (int i = strlen(s); i > 0; i--) {
  6. s[2*i] = s[i];
  7. s[2*i-1] = ' '; // 前置空格
  8. }
  9. }
  10. int main () {
  11. char str[N];
  12. printf("Please input string:");
  13. scanf("%s", str);
  14. spaceJoin(str);
  15. printf("Output %s??\n", str); // 验证后面是否有多余空格
  16. return 0;
  17. }

编译运行:注意看输出后面是有多余空格的 :::success b12@PC:~/chapter7$ gcc -Wall ./src/spaceJoin.c -o ./bin/spaceJoin
b12@PC:~/chapter7$ ./bin/spaceJoin
Please input string:1990
Output 1 9 9 0 ?? ::: 解决办法也很简单,直接将原数组 课后习题 - 图12单独处理即可。

  1. void spaceJoin(char s[]) {
  2. int n = strlen(s);
  3. s[2*n-1] = '\0'; // 末尾的'\0'
  4. for (int i = n - 1; i > 0; i--) { // 从最后一个字符开始
  5. s[2*i] = s[i];
  6. s[2*i-1] = ' '; // 前置空格
  7. }
  8. }

编译运行:注意看输出后面是无多余空格的 :::success b12@PC:~/chapter7$ gcc -Wall ./src/spaceJoin.c -o ./bin/spaceJoin
b12@PC:~/chapter7$ ./bin/spaceJoin
Please input string:2020
Output 2 0 2 0?? :::

上述实现的条件是原数组足够长,所以可以在原数组上不断进行操作。但是通常实现 'fillchar'.join(str) 函数都是返回新的字符串。简单实现代码如下:(只允许 fillchar 为字符型,而非字符数组)。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 100
  4. void join(char *dest, char *src, char fillchar) {
  5. int n = strlen(src);
  6. dest[0] = src[0]; // 第一字符单独处理,其余' c'格式写入dest
  7. for (int i = 1; i < n; i++) {
  8. dest[2*i] = src[i];
  9. dest[2*i-1] = fillchar;
  10. }
  11. }
  12. int main () {
  13. char str1[N], str2[2 * N]; // 注意是两倍长度
  14. printf("Please input string:");
  15. scanf("%s", str1); // 要求不含空格
  16. getchar(); // 接受多余空格
  17. printf("Please input fillchar:");
  18. char fillchar = getchar();
  19. join(str2, str1, fillchar);
  20. printf("Output %s??\n", str2);
  21. return 0;
  22. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/join.c -o ./bin/join
b12@PC:~/chapter7$ ./bin/join
Please input string:2046598790
Please input fillchar:&
Output 2&0&4&6&5&9&8&7&9&0?? :::

9、编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。

解题思路:与之前数组的题目类似,出题非常牵强,将 letter , digitspaceothers 换成全局变量后就为了使得在函数内部实现变量的改变。参考答案在函数中修改,然后回到 main 函数打印修改的全局变量的值。这样做还不如如下直接实现在该统计函数内部直接打印值就得了,全局变量方便在其他函数内改变,但是非常危险。
PTA函数—指针

10、写一个函数,输入一行字符,将此字符串中最长的单词输出

解题思路:本题关键是如何分隔单词,标准的统计每个单词的长度然后记录最大值和其在数组中的位置,然后输出。(书本中错误统计,如果是字符就该直接 len++ ,而不是在非第一次进行相加,虽然结果上是每个单词缺少头部并且其对于非字母单词也不是必须要求大于最长的才变为 len=0 而是只要是非字母单词就重置长度)。
另外一个就是后效性问题,往往因为最后一个单词不是因为空格结尾,所以每次循环结束后还需要再更新一次,否则会把最后的单词给漏掉。而作者就是使用 课后习题 - 图13把最后一起考虑入内。

  1. #include <stdio.h>
  2. #define N 100
  3. int isAlpha(char ch) {
  4. return ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z');
  5. }
  6. int longestWord(char str[]) {
  7. // 函数找到最长的单词然后返回其在数组中的起始位置
  8. int isSpace = 1, idx = -1, start = -1, len = 0, maxLen = 0;
  9. for (int i = 0; '\0' != str[i]; i++) {
  10. if (isAlpha(str[i])) { // 如果当前是字母
  11. if (isSpace) { // (1)若是开头
  12. idx = i;
  13. isSpace = 0;
  14. }
  15. len++; // 长度增加
  16. } else {
  17. isSpace = 1; // 遇到非字母
  18. if (len >= maxLen) {
  19. maxLen = len;
  20. start = idx;
  21. }
  22. len = 0; // 清空单词长度
  23. }
  24. }
  25. // 任然需要注意最后一个单词没有遇到空格而结束
  26. if (len >= maxLen) {
  27. maxLen = len;
  28. start = idx;
  29. }
  30. return start;
  31. }
  32. int main() {
  33. char str[N];
  34. printf("Please input a string:");
  35. fgets(str, N, stdin); // 虽然有\n,但是没关系
  36. printf("The longest word is:");
  37. for (int i = longestWord(str); isAlpha(str[i]); i++) {
  38. putchar(str[i]);
  39. }
  40. putchar('\n');
  41. return 0;
  42. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/longestWord.c -o ./bin/longestWord
b12@PC:~/chapter7$ ./bin/longestWord
Please input a string:I am a student.
The longest word is:student
b12@PC:~/chapter7$ ./bin/longestWord
Please input a string:You guess what happend
The longest word is:happend ::: 本质上有 strtok 函数可以替代,但是其会破坏字符串并且当两个相邻的分隔符在一起不会产生空串

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 100
  4. char *longestWord(char str[]) {
  5. // 函数找到最长的单词然后返回其在数组中的起始位置
  6. int maxLen = 0;
  7. char *token = strtok(str, " "), *start; // 获取第一个子字符串
  8. while (NULL != token) {
  9. printf("%s\n", token);
  10. if (maxLen <= strlen(token)) {
  11. start = token; // 保留最长字符串起始地址
  12. }
  13. token = strtok(NULL, " "); // 获得后续子字符串
  14. }
  15. return start;
  16. }
  17. int main() {
  18. char str[N];
  19. printf("Please input a string:");
  20. fgets(str, N, stdin); // 虽然有\n,但是没关系
  21. printf("The longest word is: %s\n", longestWord(str));
  22. printf("Original string: %s\n", str);
  23. return 0;
  24. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/longestWord.c -o ./bin/longestWord
b12@PC:~/chapter7$ ./bin/longestWord
Please input a string:I am a student.
I
am
a
student.
(这里空格是fgets引起的)
The longest word is: student.
(这里空格是fgets引起的,最长单词因为分隔符是空格导致后续后多余的非字母)
Original string: I
b12@PC:~/chapter7$ ./bin/longestWord
Please input a string:I am a student
I
am
a
student
The longest word is: student
Original string: I :::

11、写一个函数,用“起泡法”对输入的 10 个字符按由小到大顺序排列。

解题思路:同数字排序一样,因为字符本质也是 ASCII 码比较,因此搬用之前冒泡法即可。
书本上错误示例:当用户输入超过 10 个字符时,字符数组早就溢出,它居然还用 strlen(str) 判断?这种情况下可以限制输入字符个数或者用 while (getchar()) 函数统计先)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define N 11
  4. void bubbleSort(char str[]) {
  5. int n = strlen(str);
  6. for (int i = 0; i < n - 1; i++) { // 共比较 n-1 趟
  7. for (int j = 0; j < n - i - 1; j++) { // 一趟打 n-i-1 人
  8. if (str[j] > str[j+1]) { // 大的往后冒
  9. char tmp = str[j];
  10. str[j] = str[j+1];
  11. str[j+1] = tmp;
  12. }
  13. }
  14. printf("Round %d:%s\n", i + 1, str);
  15. }
  16. }
  17. int main() {
  18. char str[N];
  19. printf("Please input %d char:", N - 1);
  20. scanf("%10s", str); // 最多接受10个字符,然后外加'\0'
  21. // fgets(str, N, stdin); // 不用的原因:有\n或者没有\n
  22. printf("Original string: %s\n", str);
  23. bubbleSort(str);
  24. printf("Sort: %s\n", str);
  25. return 0;
  26. }

编译运行:(将具体交换细节打印出来) :::success b12@PC:~/chapter7$ gcc -Wall ./src/bubbleSort.c -o ./bin/bubbleSort
b12@PC:~/chapter7$ ./bin/bubbleSort
Please input 10 char:9876543210A # 多一位A,不会被读入
Original string: 9876543210
Round 1:8765432109
Round 2:7654321089
Round 3:6543210789
Round 4:5432106789
Round 5:4321056789
Round 6:3210456789
Round 7:2103456789
Round 8:1023456789
Round 9:0123456789
Sort: 0123456789 :::

12、用牛顿迭代法求根。方程为课后习题 - 图14,系数a,b,c,d的值依次为1,2,3,4,由主函数输入。求x在1附近的一个实根。求出根后由主函数输出。

解题思路课后习题 - 图15的导数为 课后习题 - 图16,由 课后习题 - 图17得知原函数单调递增,其图像如下所示
image.png
因此满足牛顿迭代要求连续的条件,并且从图中看到题目给假设 1 开始迭代是没错的。由牛顿迭代公式 课后习题 - 图19 得:
image.png

  1. #include <stdio.h>
  2. #include <math.h>
  3. double solve(double a, double b, double c, double d) {
  4. double x1, x0 = 1, f1, f0;
  5. while (1) {
  6. f0 = ((a * x0 + b) * x0 + c) * x0 + 4; // 函数值
  7. f1 = (3 * a * x0 + 2 * b) * x0 + c; // 求导
  8. x1 = x0 - f0 / f1;
  9. if (fabs(x1 - x0) < 1e-3) break;
  10. x0 = x1;
  11. }
  12. return x1;
  13. }
  14. int main() {
  15. printf("Please input a, b, c, d:");
  16. double a, b, c, d;
  17. scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
  18. printf("x = %10.7f\n", solve(a, b, c, d));
  19. return 0;
  20. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/solveFormula.c -o ./bin/solveFormula -lm
b12@PC:~/chapter7$ ./bin/solveFormula
Please input a, b, c, d:1 2 3 4
x = -1.6506292 ::: 这里求导很机械,因为知道你预先输入什么,如果需要对任何函数进行求导,那么要进行很多判断,一般使用链表实现。

13、用递归方法求 n 阶勒让德多项式的值,递归公式为

课后习题 - 图21
解题思路:方程式已经给出,直接条件判断递归调用即可(体现函数入栈出栈)
image.png

  1. #include <stdio.h>
  2. double PnX(int n, double x) {
  3. if (0 == n) {
  4. return 1;
  5. } else if (1 == n) {
  6. return x;
  7. }
  8. return ((2 * n - 1) * x - PnX(n - 1, x) - (n - 1) * PnX(n - 2, x)) / n;
  9. }
  10. int main() {
  11. printf("Please input n, x:");
  12. double x;
  13. int n;
  14. scanf("%d %lf", &n, &x);
  15. printf("P%d(%f) = %10.7f\n", n, x, PnX(n, x));
  16. return 0;
  17. }

编译运行:(书本上最后的大于等于 1 错误方程表达式) :::success b12@PC:~/chapter7$ gcc -Wall ./src/PnX.c -o ./bin/Pnx
b12@PC:~/chapter7$ ./bin/Pnx
Please input n, x:0 7
P0(7.000000) = 1.0000000
b12@PC:~/chapter7$ ./bin/Pnx
Please input n, x:1 2
P1(2.000000) = 2.0000000
b12@PC:~/chapter7$ ./bin/Pnx
Please input n, x:3 4
P3(4.000000) = 2.8333333 :::

14、输入 10 个学生 5 门课的成绩,分别用函数实现下列功能:

  1. 计算每个学生的平均分
  2. 计算每门课的平均分
  3. 找出所有 50 个分数中最高的分数所对应的学生和课程
  4. 计算平均分方差:课后习题 - 图23,其中 课后习题 - 图24 为某一学生的平均分。

解题思路:本题没有难度,全是体力活。用数组 double score[N][M] 记录所有人的成绩。

  1. 每个学生的平均分用数组 double averStu[N] 一个元素表示一个学生的 M 门科目的平均分,如下方图片黄色的列
  2. 每门课的平均分用数组 double averSub[M] 一个元素表示一个学生的 M 门科目的平均分,如下方图片橙色的行
  3. 找出所有 50 个分数中最高的分数所对应的学生和课程:关键要记录下所在成绩单的位置,使用全局变量 row,col 记录。
  4. 平均分方差公式已给出,直接计算。

![WKNL$A$FX)QD1`6IS15LUR.png

  1. #include <stdio.h>
  2. #define N 10 // 10名学生,二维数组行
  3. #define M 5 // 5门科目,二维数组列
  4. double score[N][M]; // N行(人)成绩单,一行是该学生所有M门科目成绩
  5. double averStu[N]; // N名学生所有M门科目平均分
  6. double averSub[M]; // M门科目平均分
  7. int row = 0, col = 0; // 最高学生成绩在成绩单中的位置
  8. void studentAverage() {
  9. /* 计算 N 名学生所有 M 门科目平均分:行平均值 */
  10. for (int i = 0; i < N; i++) {
  11. double aver = 0.0;
  12. for (int j = 0; j < M; j++) {
  13. aver += score[i][j];
  14. }
  15. averStu[i] = aver / M;
  16. }
  17. }
  18. void subjectAverage() {
  19. /* 计算 M 门科目平均分:列平均值 */
  20. for (int j = 0; j < M; j++) {
  21. double aver = 0.0;
  22. for (int i = 0; i < N; i++) {
  23. aver += score[i][j];
  24. }
  25. averSub[j] = aver / N;
  26. }
  27. }
  28. void getHigh() {
  29. /* 求出平均分最高的学生并修改全局变量行下标 */
  30. for (int i = 0; i < N; i++) {
  31. for (int j = 0; j < M; j++) {
  32. if (score[i][j] > score[row][col]) {
  33. row = i;
  34. col = j;
  35. }
  36. }
  37. }
  38. }
  39. double meanVariance() {
  40. double x2Sum = 0.0, xSum = 0.0;
  41. for (int i = 0; i < N; i++) {
  42. x2Sum += averStu[i] * averStu[i];
  43. xSum += averStu[i];
  44. }
  45. return (x2Sum / N - (xSum / N) * (xSum / N));
  46. }
  47. int main() {
  48. // 1.从 stdin 输入数据
  49. for (int i = 0; i < N; i++) {
  50. for (int j = 0; j < M; j++) {
  51. scanf("%lf", &score[i][j]);
  52. }
  53. }
  54. // 2.计算 N 名学生 M 门科目平均分
  55. studentAverage();
  56. // 3.计算 M 门科目平均分
  57. subjectAverage();
  58. // 4.求出最高成绩(平均分最高)学生
  59. getHigh();
  60. printf("\nNo.\tcourse1\tcourse2\tcourse3\tcourse4\tcourse5\taverage\n");
  61. // 5.输出 10名学生成绩 + 其平均分
  62. for (int i = 0; i < N; i++) {
  63. printf("No %d\t", i + 1);
  64. for (int j = 0; j < M; j++) {
  65. printf("%.2f\t", score[i][j]);
  66. }
  67. printf("%.2f\n", averStu[i]);
  68. }
  69. // 6.输出 M 门科目的平均分
  70. printf("\nSubject average: ");
  71. for (int j = 0; j < M; j++) {
  72. printf("%.2f\t", averSub[j]);
  73. }
  74. // 7.输出最高成绩和数组中的位置
  75. printf("\nHighest: %.2f\tNo. %d course %d\n",
  76. score[row][col], row + 1, col + 1);
  77. printf("avariance: %.2f\n", meanVariance());
  78. return 0;
  79. }

编译运行:(tips:运行过程中创建输入文件 score.txt ,然后使用 Linux 的管道命令,即 scanf 函数从 stdin 获得学成绩,甚至还可以将 stdout 重定向到文件内) :::success b12@PC:~/chapter7$ gcc -Wall ./src/scoreSheet.c -o ./bin/scoreSheet
b12@PC:~/chapter7$ cat ./src/score.txt | ./bin/scoreSheet
No. course1 course2 course3 course4 course5 average
No 1 87.00 88.00 92.00 67.00 78.00 82.40
No 2 88.00 86.00 87.00 98.00 90.00 89.80
No 3 76.00 75.00 65.00 65.00 78.00 71.80
No 4 67.00 87.00 60.00 90.00 67.00 74.20
No 5 77.00 78.00 85.00 64.00 56.00 72.00
No 6 76.00 89.00 94.00 65.00 76.00 80.00
No 7 78.00 75.00 64.00 67.00 77.00 72.20
No 8 77.00 76.00 56.00 87.00 85.00 76.20
No 9 84.00 67.00 78.00 76.00 89.00 78.80
No 10 86.00 75.00 64.00 69.00 90.00 76.80
Subject average: 79.60 79.60 74.50 74.80 78.60
Highest: 98.00 No. 2 course 4
avariance: 28.71 :::

15、写几个函数:

  1. 输入 10 个职工的姓名和职工号
  2. 按职工号由小到大顺序排序,姓名顺序也随之调整
  3. 要求输入一个职工号,用折半查找法找出该职工的姓名,从主函数输入要查找的职工号,输出该职工姓名。

解题思路:本例也是体力活,就是敲代码。使用两个数组对员工信息记录(因为数组元素必须是同一类型的,字符数组和员工整型 id 是不能混的。当学到结构体就可以创建一个包含 int id, char *name 的结构体)

  1. 职工号使用数组 int number[N] 记录,的姓名使用数组 char name[N][8] 记录,因此两者对应的关系就是数组下标对应一个员工的信息。
  2. 按职工号由小到大顺序排序:意味着上面对应的数组 char name[N][8] 也要改变位置。
  3. 因为通过第 2 步进行排序后可以使用二分查找法进行查找 int number[n] 的索引位置 idx ,然后打印输出 name[idx] 即可。 ```c

    include

    include

    define N 10 // 10名员工

void sort(int number[], char name[][N]) { / 使用选择排序对 number 内元素交换。同时改变 name 中位置 / char tmpStr[N]; for (int i = 0; i < N - 1; i++) { int idx = i; for (int j = i + 1; j < N; j++) { if (number[idx] > number[j]) { idx = j; // 记录此轮最小的 } } if (idx != i) { // 交换 int tmp = number[idx]; number[idx] = number[i]; number[i] = tmp; strcpy(tmpStr, name[idx]); // 因为二维数组不可以直接交换 strcpy(name[idx], name[i]); strcpy(name[i], tmpStr); } } }

int bisect(int number[], int left, int right, int target) { while (left <= right) { int mid = (right - left) / 2 + left; if (number[mid] < target) { left = mid + 1; } else if (number[mid] == target) { return mid; } else { right = mid - 1; } } return -1; }

int main() { int number[N], x; char name[N][N], choose; // 1.从 stdin 输入数据 for (int i = 0; i < N; i++) { scanf(“%d %9s”, number + i, name[i]); // 最长9字符 } sort(number, name); // 2.打印职工信息 for (int i = 0; i < N; i++) { printf(“%2d\t%s\n”, number[i], name[i]); // 最长9字符 } // 3.交互式查询,注意 %c 清空 while (1) { printf(“Please input number to look for:”); scanf(“%d”, &x); // 注意有\n留在缓冲区 int idx = bisect(number, 0, N - 1, x); if (-1 == idx) { printf(“No found\n”); } else { printf(“No.%d is %s\n”, x, name[idx]); } printf(“continue or not(Y/N):”); getchar(); // 重点!!! choose = getchar(); if (‘N’ == choose || ‘n’ == choose) break; } return 0; }

  1. :::success
  2. b12@PC:~/chapter7$ ./bin/employeeSheet<br />3 Li<br />1 Zhang<br />27 Yang<br />7 Qian<br />8 Sun<br />12 Jiang<br />6 Zhao<br />23 Shen<br />2 Wang<br />26 Han<br /> 1 Zhang<br /> 2 Wang<br /> 3 Li<br /> 6 Zhao<br /> 7 Qian<br /> 8 Sun<br />12 Jiang<br />23 Shen<br />26 Han<br />27 Yang<br />Please input number to look for:3<br />Search 3<br />No.3 is Li<br />continue or not(Y/N):y<br />Please input number to look for:13<br />Search 13<br />No found<br />continue or not(Y/N):n
  3. :::
  4. <a name="JQCQK"></a>
  5. # 16、写一个函数,输入一个十六进制数,输出相应的十进制数。
  6. 解题思路:参考答案中分为两步,一是数据输入有效性检测,而是实现 `hex2dec` 函数。
  7. 1. IO 有效性检测:共有三个约束条件:
  8. 1. 输入结束
  9. 1. 合法字符判断: `0-9a-fA-F` ;非法字符,例如 `z`
  10. 1. 有效长度为 `1000` ,完全不可能这么长, `int` 早就溢出。最大的也不过是无符号整型 `0xffffffff` 长度也就是 `8` 位。
  11. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1438957/1602660470836-1452a268-3392-46e0-af1e-f3c942f18b04.png#height=303&id=cE6cf&margin=%5Bobject%20Object%5D&name=image.png&originHeight=326&originWidth=651&originalType=binary&size=102326&status=done&style=none&width=606)
  12. 2. `hex2dec` 函数实现:使用秦久算法 ![](https://cdn.nlark.com/yuque/__latex/5b328cca1e4a0e673cd0f5130dcecad5.svg#card=math&code=a_n%20%3D%20base%20%5Ctimes%20%28a_n-1%20%2B%20c%29&height=20&id=U4cqH).关键就是判断大小写字母问题(此处可以在上面IO 输入时候进行全部转化为小写)
  13. ![image.png](https://cdn.nlark.com/yuque/0/2020/png/1438957/1602660848313-b02825b6-7fa6-4db7-b4b1-574e3de3d5c4.png#height=322&id=w3c5S&margin=%5Bobject%20Object%5D&name=image.png&originHeight=342&originWidth=651&originalType=binary&size=95000&status=done&style=none&width=613)
  14. ```c
  15. #include <stdio.h>
  16. #define N 9 // 最大 unsigned int 对应16进制长度
  17. unsigned int hex2dec(char s[]) {
  18. unsigned int res = 0;
  19. for (int i = 0; '\0' != s[i]; i++) {
  20. if ('0' <= s[i] && s[i] <= '9') {
  21. res = res * 16 + s[i] - '0';
  22. } else if ('a' <= s[i] && s[i] <= 'f') {
  23. res = res * 16 + (s[i] - 'a' + 10);
  24. } else {
  25. res = res * 16 + (s[i] - 'A' + 10);
  26. }
  27. }
  28. return res;
  29. }
  30. int main() {
  31. char s[N] = {'\0'};
  32. while (1) {
  33. printf("Please input hex number(>0):");
  34. char ch = getchar(), choice;
  35. int idx = 0, invalid = 0; // idx填充字符数组
  36. while ('\n' != ch && idx < N) {
  37. if (('0' <= ch && ch <= '9') ||
  38. ('a' <= ch && ch <= 'f') ||
  39. ('A' <= ch && ch <= 'F')) {
  40. s[idx++] = ch;
  41. } else {
  42. invalid = 1; // 标记非法
  43. break;
  44. }
  45. ch = getchar();
  46. }
  47. s[idx] = '\0'; // 由于连续输入,必须截断
  48. if (invalid) { // 这里就不检测长度合法,直接截取
  49. printf("Invalid character %c\n", ch);
  50. } else {
  51. printf("Decimal: %u\n", hex2dec(s));
  52. }
  53. printf("Continue or not(Y/N):");
  54. choice = getchar(); // 注意之前的\n被接受
  55. if ('n' == choice || 'N' == choice) {
  56. break;
  57. }
  58. getchar(); // 吸收上面选择的换行
  59. }
  60. return 0;
  61. }

编译运行:(书本上搞得那么麻烦的判断,真的无语,任何连续输入都有 while(1) 框架,它非要杂糅在一起) :::success b12@PC:~/chapter7$ gcc -Wall ./src/hex2dec.c -o ./bin/hex2dec
b12@PC:~/chapter7$ ./bin/hex2dec
Please input hex number(>0):a11
Decimal: 2577
Continue or not(Y/N):y
Please input hex number(>0):10
Decimal: 16
Continue or not(Y/N):y
Please input hex number(>0):f
Decimal: 15
Continue or not(Y/N):n :::

17、用递归法将一个整数n转换成字符串。例如,输入483,应输出字符串“483”。n的位数不确定,可以是任意位数的整数。

解题思路:递归最重要的是找到递归链+终止点.最难的部分就是终止点. 例如本题:很容易想到被除数为 0 的时候就是结束点,但是事实上是不完全的,当输入数据是 0 ,那么我们就漏下这个数字了,也就是它要同 do-while 循环一样,一定要执行一次。而改为 x / 10 == 0 判断即可解决这种问题。

  1. #include <stdio.h>
  2. void int2str(int n) {
  3. if (0 != n / 10) {
  4. int2str(n / 10);
  5. }
  6. putchar(n % 10 + '0'); // 后序遍历
  7. putchar(' '); // 后序遍历
  8. }
  9. int main() {
  10. int num;
  11. printf("Please input a number:");
  12. scanf("%d", &num);
  13. if (num < 0) {
  14. printf("- ");
  15. int2str(-num);
  16. } else {
  17. int2str(num);
  18. }
  19. printf("\n");
  20. return 0;
  21. }

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/int2str.c -o ./bin/int2str
b12@PC:~/chapter7$ ./bin/int2str
Please input a number:123456789
1 2 3 4 5 6 7 8 9
b12@PC:~/chapter7$ ./bin/int2str
Please input a number:-854137
- 8 5 4 1 3 7
b12@PC:~/chapter7$ ./bin/int2str
Please input a number:0
0 :::

18、给出年、月、日,计算该日是该年的第几天。

解题思路:这种题目可以直接使用Linux自带的 date 命令直接得到.

b12@PC:~/chapter7$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING          display time described by STRING, not 'now'
      --debug                annotate the parsed date,
                              and warn about questionable usage to stderr
FORMAT controls the output.  Interpreted sequences are:
  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)

b12@PC:~/chapter7$ date -d 20201014 +%j
288

但是实现起来也非常简单,从第一个月到第 n-1 月累加再加上第 n 个月第 x 天就是答案,但是需要判断闰年,因为二月多少一天关系到计算结果.

#include <stdio.h>

int sumDay(int year, int month, int day) {
    int Month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
        Month[2]++;  // 闰年多一天
    }
    for (int i = 1; i < month; i++) {
        day += Month[i];
    }
    return day;
}


int main() {
    int year, month, day;
    printf("Please input year, month, day:");
    scanf("%d %d %d", &year, &month, &day);
    printf("%d/%d/%d is the %d day in this year\n",
        year, month, day, sumDay(year, month, day));
    return 0;
}

编译运行: :::success b12@PC:~/chapter7$ gcc -Wall ./src/sumDay.c -o ./bin/sumDay
b12@PC:~/chapter7$ ./bin/sumDay
Please input year, month, day:2008 8 8
2008/8/8 is the 221 day in this year
b12@PC:~/chapter7$ date -d 20080808 +%j
221
b12@PC:~/chapter7$ ./bin/sumDay
Please input year, month, day:2001 7 9
2001/7/9 is the 190 day in this year
b12@PC:~/chapter7$ date -d 20010709 +%j
190 :::