广东工业大学anyview数据结构(2024)第一章参考答案

注:请注意题号,题号对应原题目序号末尾两位数字,下面内容不包含例题,anyview系统会对代码进行查重,提交前请修改变量,或添加变量名,又或者添加注释等加大代码差异化的操作。以下内容仅为个人手打代码,不代表最终答案,仅供参考。

其他章节参考答案

目录

06.将三个整数按升序重新排列

08.求一元多项式的值

11.求k阶裴波那契序列的第m项的值

18.计算i!×2^i的值

20.按照特定顺序打印“学生”结构体数组中所有学生的姓名

21.查找学生结构体数组中的最高成绩

22.打印“学生”结构体数组中第一个最高成绩学生的姓名和成绩

23.打印“学生”结构体数组中最后一个最高成绩学生的姓名和成绩

30.将结构体中的字符串逆序放置

49.由一维数组构建一个序列

61.构建一个值为x的结点

63.构建长度为2且两个结点的值依次为x和y的链表

65.构建长度为2的升序链表

06.将三个整数按升序重新排列
#include <algorithm>
void Descend(int &a, int &b, int &c)  // 通过交换,令 a >= b >= c
{   // Add your code here
    int k[3];
    k[0] = a, k[1] = b, k[2] = c;
    sort(k,k+3);
    a = k[2];
    b = k[1];
    c = k[0];
}
08.求一元多项式的值
#include <math.h>
float Polynomial(int n, int a[], float x0) 
{   // Add your code here
    int i,k;
    double P=0,N=1.0;
    for(i=0;i<=n;i++)
    {
    for(k=0;k<i;k++) N*=x0;
    P+=a[i]*N;
    N=1.0;
    }
    return P;
}
11.求k阶裴波那契序列的第m项的值
Status Fibonacci(int k, int m, int &f) { 
    // Add your code here
    if(k-2<0||m<0) return ERROR;
    else if(m<k-1){
        f=0;
        return OK;
    }
    else{
        int i,n,P=0,F[100];
        for(i=0;i<=k-2;i++) F[i]=0;
        F[k-1]=1;
        for(i=k;i<=m;i++){
            for(n=i-1;n>=i-k;n--) P+=F[n];
            F[i]=P;
            P=0;
        }
        f=F[m];
        return OK;
    }
}
18.计算i!×2^i的值
#include <math.h>
typedef long long ll;
Status Series(int a[], int n) { 
    // Add your code here
    if(n==0) return ERROR;
    for(int i=0;i<n;i++){
        ll k=1;
        for(int j=2;j<=i+1;j++) k*=j;
        if(k>MAXINT) return EOVERFLOW;
        k*=pow(2,i+1);
        if(k>MAXINT or k<0) return EOVERFLOW;
        a[i] = k;
    }
    return OK;
}
20.按照特定顺序打印“学生”结构体数组中所有学生的姓名
void printName(stuType student[], int index[], int n)
{  // Add your code here
    for (int i = 0; i < n; i++) {
        printf("%s\n",student[index[i]].name);
    }
}
21.查找学生结构体数组中的最高成绩
float highestScore(stuType *student[], int n)
/* 返回最高成绩  */
{  // Add your code here
    float maxn = 0.0;
    for(int i=0;i<n;i++){
        if(student[i]->score>maxn) {
            maxn=student[i]->score;
        }
    }
        
    return maxn;
}
22.打印“学生”结构体数组中第一个最高成绩学生的姓名和成绩
#include <iostream>
#include <iomanip>
void printFirstName_HighestScore(stuType *student[], int n)
{  // Add your code here
    int pos=0;
    float maxn = 0;
    for(int i=0;i<n;i++) {
        if(student[i]->score>maxn) {
            pos=i;
            maxn = student[i]->score;
        }
    }
    for(int i=0;i<4 and student[pos]->name[i];i++){
        printf("%c",student[pos]->name[i]);
    }
    printf("\n");
    printf("%.2f\n",student[pos]->score);

}
23.打印“学生”结构体数组中最后一个最高成绩学生的姓名和成绩
void printLastName_HighestScore(stuType *student[], int n)
{  // Add your code here

    int pos=0;
    float maxn=0;
    for (int i = 0; i < n; i++) {
        if(student[i]->score >= maxn) {
            maxn = student[i]->score;
            pos = i;
        }
    }

    for(int i=0;i<4 and student[pos]->name[i];i++){
        printf("%c",student[pos]->name[i]);
    }
    printf("\n");
    printf("%.2f\n",student[pos]->score);
}
30.将结构体中的字符串逆序放置
StrSequence* reverseStr(StrSequence* strSeq)
/*返回一个结构体,该结构体将strSeq中的字符串逆序存放*/
{  
    //按照题目要求,重新建立一个StrSequence容器存放结果并返回
    int n = strSeq->length;
    StrSequence *ch = (StrSequence *)malloc(sizeof(StrSequence));
    ch->length = n;
    ch->elem = (ElemType *)malloc(n*sizeof(ElemType));
    for(int i=n-1; i>=0; i--) ch->elem[n-1-i] = strSeq->elem[i];
    return ch;
    
    /*直接对strSeq修改,也可以通过题目
    for(int i=0;i<strSeq->length/2;i++){
        char c = strSeq->elem[i];
        strSeq->elem[i] = strSeq->elem[strSeq->length-i-1];
        strSeq->elem[strSeq->length-i-1] = c;
    }
    return strSeq;
    */
}
49.由一维数组构建一个序列
Status CreateSequence(Sequence &S, int n, ElemType *a) { 
   // Add your code here
   if(n<=0) return ERROR;
   S.elem=(ElemType *)malloc(sizeof(ElemType)*n);
   for(int i=0;i<n;i++){
      S.elem[i] = a[i];
   }
   S.length  = n;
   return OK;
}
61.构建一个值为x的结点
#include <stdlib.h>
LinkList MakeNode(ElemType x) { 
    // Add your code here
    LNode * node = (LNode *) malloc (sizeof(LNode));
    node->data = x;
    node->next = NULL;
    return node;
}
63.构建长度为2且两个结点的值依次为x和y的链表
LinkList CreateLinkList(ElemType x, ElemType y) { 
    // Add your code here
    LNode *node1 = (LNode *)malloc(sizeof(LNode));
    LNode *node2 = (LNode *)malloc(sizeof(LNode));
    node1->data = x;
    node2->data = y;
    node1->next = node2;
    node2->next = NULL;

   return node1; // This is a temporary code. Change it if necessary.
}
65.构建长度为2的升序链表
#include <math.h>
LinkList CreateOrdLList(ElemType x, ElemType y) { 
    // Add your code here
    int data = min(x,y);
    y = max(x,y);
    x = data;
    LNode *p1 = (LNode *) malloc (sizeof(LNode));
    LNode *p2 = (LNode *) malloc (sizeof(LNode));
    p1->data = x;
    p2->data = y;
    p1->next = p2;
    p2->next = NULL;
    return p1;
}

猜你喜欢

转载自blog.csdn.net/2301_79320558/article/details/141636227
今日推荐