[关闭]
@Arbalest-Laevatain 2018-12-28T00:35:05.000000Z 字数 5366 阅读 1493

Anyview 数据结构 第一章

数据结构习题


【题目】试写一算法,如果三个整数a,b和c的值

不是依次非递增的,则通过交换,令其为非递增。

  1. void Descend(int &a, int &b, int &c)
  2. /* 通过交换,令 a >= b >= c */
  3. {
  4. if (a<=b)
  5. {
  6. int t=a;
  7. a=b;
  8. b=t;
  9. if (b<=c)
  10. {
  11. int t1=b;
  12. b=c;
  13. c=t1;
  14. if (a<=b)
  15. {
  16. int t2=a;
  17. a=b;
  18. b=t2;
  19. }
  20. }
  21. }
  22. }

【题目】试编写算法求一元多项式

P(x) = a0 + a1x + a2x^2 + ... + anx^n

的值P(x0),并确定算法中每一语句的执行次数和整个算法
的时间复杂度。

  1. float Polynomial(int n, int a[], float x)
  2. /* 求一元多项式的值P(x)。 */
  3. /* 数组a的元素a[i]为i次项的系数,i=0,...,n */
  4. {
  5. int i, j;
  6. float t;
  7. float sum = 0.0;
  8. for (i = 2; i<n+1; i++)
  9. {
  10. t = x;
  11. for (j = 1; j<i; j++)
  12. t *= x;
  13. sum += a[i] * t;
  14. }
  15. sum = sum + a[0];
  16. sum = sum + a[1] * x;
  17. return sum;
  18. }

测试数据:

  1. A$: P0(x) = -2-6x^1-4x^2+9x^3-7x^4
  2. A$: P0(0.947100) = -9.256912
  3. A$: P0(0.669900) = -6.518545
  4. A$: P0(1.329900) = -17.781443
  5. E$: P0(x) = -2-6x^1-4x^2+9x^3-7x^4
  6. E$: P0(0.947100) = -3.434864
  7. E$: P0(0.669900) = -4.516258
  8. E$: P0(1.329900) = 1.781132
  9. ----- Error -----

优化后的代码

  1. float Polynomial(int n, int a[], float x)
  2. /* 求一元多项式的值P(x)。 */
  3. /* 数组a的元素a[i]为i次项的系数,i=0,...,n */
  4. {
  5. float p = a[n];
  6. for (i = 1; i<=n; i++)
  7. {
  8. p = a[n-i] +
  9. }
  10. sum = sum + a[0];
  11. sum = sum + a[1] * x;
  12. return sum;
  13. }

【题目】已知k阶裴波那契序列的定义为

f(0)=0, f(1)=0, ..., f(k-2)=0, f(k-1)=1;
f(n)=f(n-1)+f(n-2)+...+f(n-k), n=k,k+1,...

试编写求k阶裴波那契序列的第m项值的函数算法,
k和m均以值调用的形式在函数参数表中出现。

  1. Status Fibonacci(int k, int m, int &f)
  2. /* 求k阶斐波那契序列的第m项的值f */
  3. {
  4. int i,j;
  5. if (k < 2 || m < 0)
  6. return ERROR;
  7. if (m < k - 1)
  8. {
  9. f = 0;
  10. return OK;
  11. }
  12. if (m == k || m == k - 1)
  13. {
  14. f = 1;
  15. return OK;
  16. }
  17. else
  18. {
  19. int* l;
  20. l = (int*)malloc(k * sizeof(int));
  21. for (i = 0; i < k - 1; i++)
  22. l[i] = 0;
  23. l[k - 1] = 1;
  24. for (i = k; i < m; i++)
  25. {
  26. for (j = k - 1; j > 0; j--)
  27. {
  28. int tmp = l[j];
  29. l[j] = l[j - 1];
  30. l[j - 1] = tmp;
  31. }
  32. int v = 0;
  33. for (j = 0; j < k; j++)
  34. v += l[j];
  35. l[k - 1] = v;
  36. f = 0;
  37. for (j = 0; j < k; j++)
  38. f += l[j];
  39. }
  40. }
  41. return OK;
  42. }

【题目】试编写算法,计算i!×2^i的值并存入数组

a[0..n-1]的第i-1个分量中 (i=1,2,…,n)。假设计
算机中允许的整数最大值为MAXINT,则当对某个k
(1≤k≤n)使k!×2^k>MAXINT时,应按出错处理。注意
选择你认为较好的出错处理方法。

  1. Status Series(int a[], int n)
  2. /* 求i!*2^i序列的值并依次存入长度为n的数组a; */
  3. /* 若所有值均不超过MAXINT,则返回OK,否则OVERFLOW */
  4. {
  5. int i,j = 1,k=1;
  6. for (i = 1;i<=n;i++)
  7. {
  8. j *= i;
  9. k *=2;
  10. if (i<n && (j*k)>= MAXINT / ((i+1)*2))
  11. return OVERFLOW;
  12. a[i-1] = j*k;
  13. }
  14. return OK;
  15. }

【题目】假设有A、B、C、D、E五个高等院校进行田径对抗赛,

各院校的单项成绩均以存入计算机并构成一张表,表中每一行
的形式为:
项目名称 性别 校名 成绩 得分
编写算法,处理上述表格,以统计各院校的男、女总分和团体
总分,并输出。
******/
相关定义:

  1. typedef enum {female,male} Sex;
  2. typedef struct{
  3. char *sport; // 项目名称
  4. Sex gender; // 性别(女:female;男:male)
  5. char schoolname; // 校名为'A','B','C','D'或'E'
  6. char *result; // 成绩
  7. int score; // 得分(7,5,4,3,2或1)
  8. } ResultType;
  9. typedef struct{
  10. int malescore; // 男子总分
  11. int femalescore; // 女子总分
  12. int totalscore; // 男女团体总分
  13. } ScoreType;
  1. void Scores(ResultType *result, ScoreType *score)
  2. /* 求各校的男、女总分和团体总分, 并依次存入数组score */
  3. /* 假设比赛结果已经储存在result[ ]数组中, */
  4. /* 并以特殊记录 {"", male, ' ', "", 0 }(域scorce=0)*/
  5. /* 表示结束 */
  6. {
  7. ResultType *r=result;
  8. ScoreType *s=score;
  9. while (NULL!=r->score)
  10. {
  11. int i=r->schoolname-'A';
  12. //
  13. if (r->gender==male)
  14. s[i].malescore+=r->score;
  15. else
  16. s[i].femalescore+=r->score;
  17. s[i].totalscore=s[i].malescore+s[i].femalescore;
  18. //printf("%c学校总分:%d\n",r->schoolname,s[i].totalscore);
  19. r++;
  20. }
  21. }
  22. int i=0;
  23. while(result[i].sport!=NULL)
  24. {
  25. switch(result[i].schoolname) /*使用switch语句记录各院校的成绩*/
  26. {
  27. case 'A':
  28. score[0].totalscore+=result[i].score;
  29. if(result[i].gender==male)
  30. score[0].malescore+=result[i].score;
  31. else
  32. score[0].femalescore+=result[i].score;
  33. break;
  34. case 'B':
  35. score[1].totalscore+=result[i].score;
  36. if(result[i].gender==male)
  37. score[1].malescore+=result[i].score;
  38. else
  39. score[1].femalescore+=result[i].score;
  40. break;
  41. case 'C':
  42. score[2].totalscore+=result[i].score;
  43. if(result[i].gender==male)
  44. score[2].malescore+=result[i].score;
  45. else
  46. score[2].femalescore+=result[i].score;
  47. break;
  48. case 'D':
  49. score[3].totalscore+=result[i].score;
  50. if(result[i].gender==male)
  51. score[3].malescore+=result[i].score;
  52. else
  53. score[3].femalescore+=result[i].score;
  54. break;
  55. case 'E':
  56. score[4].totalscore+=result[i].score;
  57. if(result[i].gender==male)
  58. score[4].malescore+=result[i].score;
  59. else
  60. score[4].femalescore+=result[i].score;
  61. break;
  62. }
  63. i++;
  64. }
  65. int j;
  66. for( j=0;j<5;j++)
  67. {
  68. printf("the school %s: ", result[i].schoolname) ; /*输出各院校的男女总分和团体总分*/
  69. printf("total: %f",&score[i].totalscore);
  70. printf("male: %f",&score[i].malescore);
  71. printf("female: %f",&score[i].femalescore);
  72. }

/******

【题目】试写一算法,对序列S的第i个元素赋以值e。

序列的类型定义为:
typedef struct {
ElemType *elem;
int length;
} Sequence;
*******/

  1. Status Assign(Sequence &S, int i, ElemType e)
  2. /* 对序列S的第i个元素赋以值e,并返回OK。 */
  3. /* 若S或i不合法,则赋值失败,返回ERROR */
  4. {
  5. if (S.elem==NULL)
  6. return ERROR;
  7. if (i > S.length)
  8. return ERROR;
  9. S.elem[i]=e;
  10. return OK;
  11. }

/******

【题目】试写一算法,由长度为n的一维数组a构建一个序列S。

序列的类型定义为:

  1. typedef struct {
  2. ElemType *elem;
  3. int length;
  4. } Sequence;

*******/

  1. Status CreateSequence(Sequence &S, int n, ElemType *a)
  2. /* 由长度为n的一维数组a构建一个序列S,并返回OK。 */
  3. /* 若构建失败,则返回ERROR */
  4. {
  5. if (n<=0)
  6. return ERROR;
  7. //Sequence *s=&S;
  8. S.length=n;
  9. S.elem=(ElemType*) malloc (n*sizeof(ElemType));
  10. int i;
  11. ElemType *p;
  12. for (i=0,p=S.elem;p<S.elem+n;p++,i++)
  13. {
  14. *p=a[i];
  15. }
  16. return OK;
  17. }

/******

【题目】链表的结点和指针类型定义如下

  1. typedef struct LNode {
  2. ElemType data;
  3. struct LNode *next;
  4. } LNode, *LinkList;

试写一函数,构建一个值为x的结点。
*******/

  1. LinkList MakeNode(ElemType x)
  2. /* 构建一个值为x的结点,并返回其指针。*/
  3. /* 若构建失败,则返回NULL。 */
  4. {
  5. LNode *p;
  6. p=(LNode*) malloc (sizeof(LNode));
  7. if (NULL==p) return NULL;
  8. p->data=x;
  9. LinkList L;
  10. L->next=p;
  11. return p;
  12. }

/******

【题目】链表的结点和指针类型定义如下

  1. typedef struct LNode {
  2. ElemType data;
  3. struct LNode *next;
  4. } LNode, *LinkList;

试写一函数,构建长度为2且两个结点的值依次为x和y的链表。
******/

  1. LinkList CreateLinkList(ElemType x, ElemType y)
  2. /* 构建其两个结点的值依次为x和y的链表。*/
  3. /* 若构建失败,则返回NULL。 */
  4. {
  5. LNode *p1, *p2;
  6. p1=(LNode*) malloc (sizeof(LNode*));
  7. p2=(LNode*) malloc (sizeof(LNode*));
  8. if (NULL==p1 || NULL==p2) return NULL;
  9. p1->data=x;
  10. p2->data=y;
  11. LinkList L;
  12. L=p1;
  13. L->next=p2;
  14. return L;
  15. }

/******

【题目】链表的结点和指针类型定义如下

  1. typedef struct LNode {
  2. ElemType data;
  3. struct LNode *next;
  4. } LNode, *LinkList;

试写一函数,构建长度为2的升序链表,两个结点的值
分别为x和y,但应小的在前,大的在后。
******/

  1. LinkList CreateOrdLList(ElemType x, ElemType y)
  2. /* 构建长度为2的升序链表。 */
  3. /* 若构建失败,则返回NULL。 */
  4. {
  5. LNode *p1,*p2;
  6. p1=(LNode*) malloc (sizeof(LNode*));
  7. p2=(LNode*) malloc (sizeof(LNode*));
  8. if (NULL==p1 || NULL==p2) return NULL;
  9. x<y ? (p1->data=x,p2->data=y) : (p1->data=y,p2->data=x);
  10. LinkList L;
  11. L=p1;
  12. L->next=p2;
  13. return L;
  14. }

https://blog.csdn.net/lzc394049722/article/details/51836980

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注