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.
    }
}
相关推荐
老赵聊算法、大模型备案3 小时前
北京市生成式人工智能服务已备案信息公告(2025年12月11日)
人工智能·算法·安全·aigc
workflower3 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger4 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者4 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
厕所博士4 小时前
红黑树原理前置理解—— 2-3 树
算法·2-3树·红黑树原理理解前置
林杜雨都4 小时前
Action和Func
开发语言·c#
皮卡龙4 小时前
Java常用的JSON
java·开发语言·spring boot·json
火山灿火山5 小时前
Qt常用控件(三)
开发语言·qt
萌>__<新5 小时前
力扣打卡每日一题————除自身外所有元素的乘积
数据结构·算法
利刃大大5 小时前
【JavaSE】十三、枚举类Enum && Lambda表达式 && 列表排序常见写法
java·开发语言·枚举·lambda·排序