title: 高级程序设计-期末复习习题组
tags:

  • C
  • 复习
    abbrlink: ecc186f9
    date: 2021-06-04 16:08:22

1. 用链表实现单词序列倒序输出

题目

用链表实现单词序列倒序输出。与以往不同,请考虑采用一种完全的动态分配方式! 为降低难度,“仁慈”的我已经给出了输出和释放的代码,你只要写出创建链表的creat函数定义就可以了。

比如输入为: abc bcd cde

则输出为: cde bcd abc

见题干!

你只能在代码输入框中:”//start(或#start)”行的下面,”//end(或#end)”行的上面输入你的代码, 而不能改变”//start(或#start)”以及其上所有行的代码,包括添加空格与空行, 也不能改变”//end(或#end)”以及其下所有行的代码,包括添加空格与空行.

代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 100
  5. struct node
  6. {
  7. char * data;
  8. struct node *next;
  9. };
  10. typedef struct node NODE;
  11. NODE* creat()
  12. {
  13. NODE *head,*cur;
  14. char in[N];
  15. //start
  16. head=(NODE *)malloc(sizeof(NODE));
  17. head->next=NULL;
  18. while(scanf("%s", in)!=EOF)
  19. {
  20. cur=(NODE *)malloc(sizeof(NODE));
  21. cur->data=(char *)malloc(sizeof(char) * N);
  22. strcpy(cur->data, in);
  23. cur->next=head->next;
  24. head->next=cur;
  25. }
  26. //end
  27. return head;
  28. }
  29. int main()
  30. {
  31. NODE *head,*cur,*pre;
  32. /*creat list*/
  33. head = creat();
  34. /*print list*/
  35. cur = head->next;
  36. while(cur!=NULL&&cur->next!=NULL)
  37. {
  38. printf("%s ",cur->data);
  39. cur = cur->next;
  40. }
  41. if(cur!=NULL)
  42. printf("%s\n",cur->data);
  43. /*release list*/
  44. while(head->next!=NULL)
  45. {
  46. pre = head;
  47. cur = head->next;
  48. while(cur->next!=NULL)
  49. {
  50. pre = cur;
  51. cur = cur->next;
  52. }
  53. free(cur->data);
  54. free(cur);
  55. pre->next = NULL;
  56. }
  57. return 0;
  58. }

2. 二值图像处理

题目

别紧张,你只把这个简单的二值图像处理的问题当成C语言的一道题目就可以。 输入为一个n_n的二维整型矩阵(矩阵中元素的最小值为0,最大值不超过1),输出其中0、1这两种值的个数。输入数 据有多组,首先输入一个整数n(0<n<10),代表矩阵的维数,接着是n_n个0或者1。

比如输入为:

8

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 0

0 0 0 0 0 1 0 0

0 0 0 0 1 0 0 0

0 0 0 1 0 0 0 0

0 0 1 0 0 0 0 0

0 1 0 0 0 0 0 0

1 0 0 0 0 0 0 0

则输出为: 53 11

代码

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main()
  4. {
  5. int n, ans0=0, ans1=0;
  6. scanf("%d", &n);
  7. for(int i=0;i<n*n;i++)
  8. {
  9. int tmp;
  10. scanf("%d", &tmp);
  11. if(!tmp) ans0++;
  12. else ans1++;
  13. }
  14. printf("%d %d", ans0, ans1);
  15. return 0;
  16. }

3.计算不同类型字符的数量

题目

如标题所示。数据数据有多组,每组为一个用空白符分隔的字符串(长度<100),请试着统计其中字母(大小写均为字 母)、数字(0~9)和其它字符的个数并输出。

比如输入为: Iloveprograming1@2#3

则输出为: 15 3 2

注意:字符串可能以’\0’或者”\r\0”作为结束标志!

见题干!

代码

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<ctype.h>
  4. int main()
  5. {
  6. char str[110];
  7. while(scanf("%s", str)!=EOF)
  8. {
  9. int ansalpha=0, ansnum=0, ansother=0;
  10. int len=strlen(str);
  11. for(int i=0;i<len;i++)
  12. {
  13. if(isalpha(str[i])) ansalpha++;
  14. else if(isalnum(str[i])) ansnum++;
  15. else ansother++;
  16. }
  17. printf("%d %d %d\n", ansalpha, ansnum, ansother);
  18. }
  19. return 0;
  20. }

PS

这道题题目里面还给了结束符的类型,应该还要做具体的判定,在这里就先不用了,到时候具体问题还需要根据调试做修改

4. 实现字符串排序

题目

实现字符串排序。与以往不同,请考虑采用一种完全的动态分配方式。请你: 1. 定义一个整型变量n,用于确定数组字符串的个数; 2. 用动态分配定义一个指针数组,用于存储每一个待排序字符串的首地址; 3. 定义一个输入缓冲区(长度不超过100的一维字符数组),用于存储不带分隔符的字符串; 4. 根据输入缓冲区中当前串的长度,动态分配空间,并将其复制到新分配的空间中; 5. 用函数实现字符串按字典升序排序,并输出排序后的字符串(每串一行); 6. 记得释放所有之前动态分配开辟的空间!

比如输入为: 3 banana apple peach

则输出为: apple banana peach

代码

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void sort(char **prelist, int n)
  5. {
  6. char tmp[110];
  7. for(int i=0;i<n;i++)
  8. {
  9. for(int j=i;j<n;j++)
  10. {
  11. if(strcmp(prelist[i], prelist[j])>0)
  12. {
  13. strcpy(tmp, prelist[i]);
  14. strcpy(prelist[i], prelist[j]);
  15. strcpy(prelist[j], tmp);
  16. }
  17. }
  18. }
  19. }
  20. int main()
  21. {
  22. int n;
  23. scanf("%d", &n);
  24. char **prelist=(char **)malloc(sizeof(char *)*n);
  25. char str[110];
  26. for(int i=0;i<n;i++)
  27. {
  28. scanf("%s", str);
  29. int len=strlen(str);
  30. char *p=((char *)malloc(sizeof(char)*len));
  31. strcpy(p, str);
  32. prelist[i]=p;
  33. }
  34. sort(prelist, n);
  35. for(int i=0;i<n;i++)
  36. {
  37. printf("%s ", prelist[i]);
  38. }
  39. return 0;
  40. }

代码(重构)

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void sort(char **prelist, int n)
  5. {
  6. char tmp[110];
  7. for(int i=0;i<n;i++)
  8. {
  9. for(int j=0;j<n;j++)
  10. {
  11. if(strcmp(prelist[i], prelist[j])<0)
  12. {
  13. strcpy(tmp, prelist[i]);
  14. strcpy(prelist[i], prelist[j]);
  15. strcpy(prelist[j], tmp);
  16. }
  17. }
  18. }
  19. }
  20. int main()
  21. {
  22. int n;
  23. scanf("%d", &n);
  24. char **prelist;
  25. prelist=(char **)malloc(sizeof(char *)*n);
  26. for(int i=0;i<n;i++)
  27. {
  28. char str[110];
  29. scanf("%s", str);
  30. char *newarr=(char *)malloc(sizeof(char) * strlen(str));
  31. strcpy(newarr, str);
  32. prelist[i]=newarr;
  33. }
  34. sort(prelist, n);
  35. for(int i=0;i<n;i++)
  36. printf("%s ", prelist[i]);
  37. return 0;
  38. }

5. 战旗游戏

题目

我知道你们男生女生都喜欢打游戏,下面这道简单的编程题是某战棋游戏的一部分,实现的功能是向上、向下、向左、 向右、向左上、向左下、向右上、向右下这八个方向移动一格。请编写一个process函数,实现对上述八个函数的调 用。 比如:

输入2 3

则输出为:3 3

代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void process(int *x, int *y, void (*fun)(int *x, int *y))
  4. {
  5. //start
  6. fun(x, y);
  7. //end
  8. }
  9. void up(int *x, int *y)
  10. {
  11. *y = *y - 1;
  12. return ;
  13. }
  14. void down(int *x, int *y)
  15. {
  16. *y = *y + 1;
  17. return ;
  18. }
  19. void left(int *x, int *y)
  20. {
  21. *x = *x - 1;
  22. return ;
  23. }
  24. void right(int *x, int *y)
  25. {
  26. *x = *x + 1;
  27. return ;
  28. }
  29. void up_left(int *x, int *y)
  30. {
  31. *x = *x - 1;
  32. *y = *y - 1;
  33. return ;
  34. }
  35. void up_right(int *x, int *y)
  36. {
  37. *x = *x + 1;
  38. *y = *y - 1;
  39. return ;
  40. }
  41. void down_left(int *x, int *y)
  42. {
  43. *x = *x - 1;
  44. *y = *y + 1;
  45. return ;
  46. }
  47. void down_right(int *x, int *y)
  48. {
  49. *x = *x + 1;
  50. *y = *y + 1;
  51. return ;
  52. }
  53. int main(void)
  54. {
  55. int x,y;
  56. while(scanf("%d%d",&x,&y)==2)
  57. {
  58. process(&x,&y,up);
  59. process(&x,&y,right);
  60. process(&x,&y,right);
  61. process(&x,&y,up_right);
  62. process(&x,&y,left);
  63. process(&x,&y,down);
  64. process(&x,&y,down_left);
  65. process(&x,&y,up_left);
  66. process(&x,&y,down_right);
  67. printf("%d %d\n",x,y);
  68. }
  69. return 0;
  70. }

6. 期末成绩统计

题目

期末大考周即将到来了。诸位要面临C语言、高数、大物的期末考试。请你编写一个程序,记录全班同学的三科成绩并 按C语言成绩的降序排序输出。 输入数据有多组:先用一个整型变量n存储班级同学的人数;接着存储这n名同学的三科(C语言、高数、大物)成绩 (类型为整型);排序后输出。 比如,

输入为: 3 张三 80 70 60 李四 90 80 70 王五 100 90 80

则输出为: 王五 100 90 80 李四 90 80 70 张三 80 70 60

代码

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define LEN 20
  5. #define N 40
  6. struct stu
  7. {
  8. char name[LEN];
  9. int scores[3];
  10. }student[N];
  11. int main(void)
  12. {
  13. int n;
  14. void input(struct stu *s,int n);
  15. void sort(struct stu *s,int n);
  16. void print(struct stu *s, int n);
  17. while(scanf("%d",&n)==1)
  18. {
  19. input(student,n);
  20. sort(student,n);
  21. print(student,n);
  22. }
  23. return 0;
  24. }
  25. void input(struct stu *s,int n)
  26. {
  27. int i,j;
  28. for(i=0;i<n;i++)
  29. {
  30. scanf("%s",s[i].name);
  31. for(j=0;j<3;j++)
  32. scanf("%d",&s[i].scores[j]);
  33. }
  34. return ;
  35. }
  36. void sort(struct stu *s,int n)
  37. {
  38. //start
  39. struct stu tmp;
  40. for(int i=0;i<n;i++)
  41. for(int j=i;j<n;j++)
  42. {
  43. if(s[i].scores[0]<s[j].scores[0])
  44. {
  45. tmp=s[i];
  46. s[i]=s[j];
  47. s[j]=tmp;
  48. }
  49. }
  50. //end
  51. }
  52. void print(struct stu *s, int n)
  53. {
  54. int i,j;
  55. for(i=0;i<n;i++)
  56. {
  57. printf("%s ", s[i].name);
  58. for(j=0;j<3;j++)
  59. if(j<2)
  60. printf("%d ",s[i].scores[j]);
  61. else
  62. printf("%d\n",s[i].scores[j]);
  63. }
  64. return ;
  65. }