Anyview数据结构第一章(按需自取)

103

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
int main()
{
	int a, b;
	//第1种交换a和b的值的方法
	printf("第1种交换a和b的值的方法:\n", a, b);
	a = 8;
	b = 10;
	printf("交换前:a = %d, b = %d\n", a, b);
	int c = b;
	b = a;
	a = c;
	printf("交换后:a = %d, b = %d\n\n", a, b);
	//第2种交换a和b的值的方法
	printf("第2种交换a和b的值的方法:\n", a, b);
	a = 8;
	b = 10;
	printf("交换前:a = %d, b = %d\n", a, b);
	a = a + b;
	b = a - b;
	a = a - b;
	printf("交换后:a = %d, b = %d\n\n", a, b);
	//第3种交换a和b的值的方法
	printf("第3种交换a和b的值的方法:\n", a, b);
	a = 8;
	b = 10;
	printf("交换前:a = %d, b = %d\n", a, b);
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("交换后:a = %d, b = %d\n", a, b);
	//试一试:在第3种交换方法中,令a和b的值相等,例如都为8,会出现什么现象?
	return 0;
}

106

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
void Descend(int &a, int &b, int &c)  // 通过交换,令 a >= b >= c
{   // Add your code here
	int *p=&a,*q=&b,*t=&c;
    if(*q<*t)
    {
		*q=*t+*q;
        *t=*q-*t;
        *q=*q-*t;
    }
    if(*p<*q)
    {
		*p=*p+*q;
        *q=*p-*q;
        *p=*p-*q;
    }
    if(*q<*t)
    {
		*q=*t+*q;
        *t=*q-*t;
        *q=*q-*t;
    }
}

108

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
float Polynomial(int n, int a[], float x0) 
{   // Add your code here
	int i;
	double p=a[0];
	for(i=1;i<=n;i++)
	{
		p+=(a[i]*pow(x0,i));
	}
	return p;
}

111

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
Status Fibonacci(int k, int m, int &f) 
{ 
    // Add your code here
	if(m<0||k<2)
    {
        return ERROR;
    }
    if(m<k-1)//k-1项前面的都是0
    {
        f=0;
        return OK;
    }
    if(m==k-1||m==k)//第k-1项和k都是1
    {
        f=1;
        return OK;
    }
    else//第k项后面的项
    {
    	int arr[m+1]={0},i,j,q;
        i=k;
        arr[k-1]=1;//第k-1项是1
        for(;i<=m;i++)//i从k项开始
        {
            for(q=0,j=i-k;j<=i;j++)
            {
                q+=arr[j];//求k到m项的数
            }
            arr[i]=q;
        }
        f=arr[m];
        return OK;
    }
}

118

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
Status Series(int a[], int n) 
{ 
    // Add your code here
    if(n<=0)//判断长度是否小于等于0
    {
        return ERROR;//返回错误
    }
	int i,j,k,x=1,y=0;
    for(i=0,j=1;i<=n-1,j<=n;i++,j++)
    {
        x=1;
        y=0;
        for(k=1;k<=j;k++)
        {
			x*=k;//累乘器
        }
        y=x*pow(2,k);//计算式子的值
        if(y>MAXINT)
        {
            return EOVERFLOW;//超过返回出错
        }
        else {
            a[i]=y;//存储不超过的值
        }
    }
    return OK;//均不超过,返回OK
}

120

objectivec 复制代码
#include "allinclude.h"
void printName(stuType student[], int index[], int n)
{  // Add your code here
	int  j;
	for(int i=0;i<n;i++)
    {
        j=index[i];
        printf("%s\n",student[j].name);
    }
}

121

objectivec 复制代码
#include "allinclude.h"
float highestScore(stuType *student[], int n)
/* 返回最高成绩  */
{  // Add your code here
    float a[n];
    for(int i=0;i<n;i++)
    {
        a[i]=student[i]->score;
    }
    float max=a[0];
    for(int i=0;i<n;i++)
    {
        if(max<a[i])
        {
            max=a[i];
        }
    }
    return max;
}

122

objectivec 复制代码
#include "allinclude.h"
void printFirstName_HighestScore(stuType *student[], int n)
{  // Add your code here
    float max=student[0]->score;
    int j=0;
    char max_name[4];
    for(int i=0;i<n;i++)
    {
        if(max<student[i]->score)
        {
            max=student[i]->score;
            j=i;
        }
    }
    strcpy(max_name,student[j]->name);
    for(int i=0;i<4;i++)
    printf("%c",max_name[i]);
    printf("\n%.2f\n",max);
}

123

objectivec 复制代码
#include "allinclude.h"
void printLastName_HighestScore(stuType *student[], int n)
{  // Add your code here
    float max=student[0]->score;
    int j=0;
    char max_name[4];
    for(int i=0;i<n;i++)
    {
        if(max<=student[i]->score)
        {
            max=student[i]->score;
            j=i;
        }
    }
    strcpy(max_name,student[j]->name);
    for(int i=0;i<4;i++)
    printf("%c",max_name[i]);
    printf("\n%.2f\n",max);
}

125

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
void A() {
	printf("X\n");
}
void B() {
	printf("Y\n");
}
int main()
{
	void (*funcp)(); //定义函数指针
	funcp = A;
	(*funcp)(); // 实际上调用了A( );
	funcp = B;
	(*funcp)(); // 实际上调用了B( );
	return 0;
}

126

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
void hello() 
{ 
	printf("Hello world!\n"); 
}
void runFun(void (*pFun)()) 
{
	pFun();  //函数指针;
}
int main()
{
	runFun(hello);  //hello是实际要调用的函数
	return 0;
}

130

objectivec 复制代码
#include "allinclude.h"
StrSequence* reverseStr(StrSequence* strSeq)
/*返回一个结构体,该结构体将strSeq中的字符串逆序存放*/
{  // Add your code here
    int n=strSeq->length;
    char a;
    for(int i=0;i<n/2;i++)
    {
        a=strSeq->elem[i];
        strSeq->elem[i]=strSeq->elem[n-i-1];
        strSeq->elem[n-i-1]=a;
    }
    return strSeq;
}

149

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
Status CreateSequence(Sequence &S, int n, ElemType *a) 
{ 
   // Add your code here
   if(n<=0)
   {
      return ERROR;
   }
   S.elem=(ElemType*)malloc(n*sizeof(ElemType));
   for(int i=0;i<n;i++)
   {
      S.elem[i]=a[i];
   }
   S.length=n;
   return OK;
}

161

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
LinkList MakeNode(ElemType x) { 
    // Add your code here
    LNode *p;
    p=(LNode*)malloc(sizeof(LNode));
    if(p==NULL)
    {
        return NULL;
    }
    else 
    {
        p->data=x;
    }
}

163

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
LinkList CreateLinkList(ElemType x, ElemType y) { 
    // Add your code here
    LNode*p;
    p=(LNode*)malloc(3*sizeof(LNode));
    if(p!=NULL)
    {
        p->data=x;
        p->next=(LNode*)malloc(3*sizeof(LNode));
        if(p->next==NULL)
        {
            return NULL;
        }
        else {
            p->next->data=y;
            p->next->next=NULL;
            return p;
        }
    }
    else
    {
   return NULL; // This is a temporary code. Change it if necessary.
    }
}

165

objectivec 复制代码
#include "allinclude.h"  //DO NOT edit this line
LinkList CreateOrdLList(ElemType x, ElemType y) { 
    // Add your code here
	LNode*p;
    p=(LNode*)malloc(3*sizeof(LNode));
    if(p!=NULL)
    {
        p->data=(x>y)?y:x;
        p->next=(LNode*)malloc(3*sizeof(LNode));
        if(p->next==NULL)
        {
            return NULL;
        }
        else {
            p->next->data=(x>y)?x:y;
            p->next->next=NULL;
            return p;
        }
    }
    else
    {
        return NULL; // This is a temporary code. Change it if necessary.
    }
}
相关推荐
RuoZoe2 小时前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
AI软著研究员4 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish4 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱5 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者21 小时前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮1 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者1 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考1 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx1 天前
CART决策树基本原理
算法·机器学习
Wect1 天前
LeetCode 210. 课程表 II 题解:Kahn算法+DFS 双解法精讲
前端·算法·typescript