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.
    }
}
相关推荐
松岛雾奈.23018 分钟前
机器学习--数据集的标准化和归一化算法;随机森林
人工智能·算法·机器学习
黑仔要睡觉21 分钟前
Anaconda和Pycharm的卸载
开发语言·python
橘颂TA26 分钟前
【剑斩OFFER】算法的暴力美学——丢失的数字
数据结构·算法·leetcode·结构与算法
努力的白熊嗨28 分钟前
大文件 Hash 计算:Web Worker 并行优化的原理与局限性
javascript·算法
lqj_本人42 分钟前
Qt与鸿蒙原生桥接实战:网络通信与数据同步问题
开发语言·qt
CoovallyAIHub1 小时前
存储风暴下的边缘智能韧性:瑞芯微RK3588如何将供应链挑战转化为市场机遇
深度学习·算法·计算机视觉
赖small强1 小时前
【Linux C/C++开发】第25章:元编程技术
linux·c语言·c++·元编程
iFlow_AI1 小时前
iFlow CLI快速搭建Flutter应用记录
开发语言·前端·人工智能·flutter·ai·iflow·iflow cli
杜子不疼.1 小时前
【C++】解决哈希冲突的核心方法:开放定址法 & 链地址法
c++·算法·哈希算法