廣工anyview數(shù)據(jù)結(jié)構(gòu)習(xí)題第一章,
在學(xué)習(xí)過(guò)程中部分題目參考了Giyn 、戮漠、雁過(guò)留痕等大佬的代碼,在此感謝。
題目解法不是最優(yōu)解,但希望能給大家有所啟發(fā)。同時(shí)也發(fā)了文檔資源,需要可自取。
如果對(duì)你有幫助,可以給卑微的博主留個(gè)贊、關(guān)注、收藏? ?(不是)?
(騙一下數(shù)據(jù),說(shuō)不定以后面試就過(guò)了,拜謝)
目錄
1.DC01PE03E
2.DC01PE06
3.DC01PE08
4.DC01PE11
5.DC01PE18
6.DC01PE20
7.DC01PE21
8.DC01PE22
9.DC01PE23
10.DC01PE25E
11.DC01PE26E
12.DC01PE30
13.DC01PE49
14.DC01PE61
15.DC01PE63
16.DC01PE65
1.DC01PE03E
【例題】學(xué)習(xí)交換兩個(gè)整型變量的值的方法。
注意∶本題目是例題,無(wú)需修改
/添加任何代碼即可正常運(yùn)行。請(qǐng)注意編譯、運(yùn)行和調(diào)試本道題目,觀察執(zhí)行過(guò)程中的變量變化情況。
#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,會(huì)出現(xiàn)什么現(xiàn)象?
return 0;
}
2.DC01PE06
試寫(xiě)一算法,如果三個(gè)整數(shù)a,b和c的值不是依次非遞增的,則通過(guò)交換,令其為非遞增。要求實(shí)現(xiàn)下列函數(shù)∶
void Descend(int &a,int &b,int &c);
/* 通過(guò)交換,令a>=b>= c*/
#include "allinclude.h" //DO NOT edit this line
void Descend(int &a, int &b, int &c) // 通過(guò)交換,令 a >= b >= c
{ // Add your code here
int d;
if(a<b)
{
d=a,a=b,b=d;
}
if(b<c)
{ if (a<c)
{
d=a,a=c,c=b,b=d;
}
else d=b,b=c,c=d;
}
}
3.DC01PE08
試編寫(xiě)算法,求一元多項(xiàng)式
P(x)= a0 + a1x + a2x^2 +...+ anx^n
的值P(x0),并確定算法中每一語(yǔ)句的執(zhí)行次數(shù)和整個(gè)算法的時(shí)間復(fù)雜度。
要求實(shí)現(xiàn)下列函數(shù)∶
float Polynomial(int n,int a[],float x0);
/*求一元多項(xiàng)式的值P(x0)。*/
/* 數(shù)組a的元素a【i】為i次項(xiàng)的系數(shù),i=0,1,...,n*/
#include "allinclude.h" //DO NOT edit this line
float Polynomial(int n, int a[], float x0)
{ // Add your code here
float result=0;
int i=0;
float x1=1;
for(;i<=n;i++)
{
result+=a[i]*x1;
x1*=x0;
}
return result;
}
4.DC01PE11
已知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,...
試編寫(xiě)求k階裴波那契序列的第m項(xiàng)值的函數(shù)算法,k和m均以值調(diào)用的形式在函數(shù)參數(shù)表中出現(xiàn)。
要求實(shí)現(xiàn)下列函數(shù)∶
Status Fibonacci(int k,int m,int &f);
/* 如果能求得k階斐波那刻序列的第m.項(xiàng)的值f,則返回0K;*/
/* 否則(比如,參數(shù)k和m 不合理)返回ERROR*/
#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)
{
f = 0;
return OK;
}
if (m == k - 1 || m == k)
{
f = 1;
return OK;
}
//m>k
int arr[m+1] , i, j;
for (i = 0; i <= m;)
arr[i++] = 0; //初始化 f(0)——f(m)都是0
i = k-1;
arr[i++] = 1; //執(zhí)行后i==k
for (int sum; i <= m; i++)
{
for (sum=0,j = i - k; j <=i; j++)
sum += arr[j];
arr[i] = sum;
}
f = arr[m];
return OK;
}
5.DC01PE18
【題目】試編寫(xiě)算法,計(jì)算i!x2^i的值并存入數(shù)組a【0..n-1】的第i-1個(gè)分量中(i=1,2,,n)。假設(shè)計(jì)算機(jī)中允許的整數(shù)最大值為MAXINT,則當(dāng)對(duì)某個(gè)k (1≤k≤n),使k!x2^k>MAXINT時(shí),應(yīng)按出錯(cuò)處理。注意選擇你認(rèn)為較好的出錯(cuò)處理方法。
要求實(shí)現(xiàn)下列函數(shù)∶
Status Series(int a[ ],int n);
/* 求i!*2^i序列的值并依次存入長(zhǎng)度為n的數(shù)組a;
若所有 值均不超過(guò)MAXINT,則返回OK,否則返回EOVERFLOW*/
#include "allinclude.h" //DO NOT edit this line
Status Series(int a[], int n) {
// Add your code here
if(n<1)
return ERROR;
int i,j = 1,k=1;
for (i = 1;i<=n;i++)
{
j *= i;
k *=2;
if(j*k>MAXINT)
return EOVERFLOW;
a[i-1] = j*k;
}
return OK;
}
6.DC01PE20
已知某表示"學(xué)生"的結(jié)構(gòu)體定義如下,編寫(xiě)算法,按照以下要求打印一個(gè)"學(xué)生"結(jié)構(gòu)體數(shù)組student中所有學(xué)生的姓名。每打印一個(gè)姓名,則換行。要求∶index[ ]中每個(gè)元素的值在0~n-1之間,對(duì)應(yīng)于student數(shù)組中各元素的下標(biāo)。請(qǐng)依次按照index[ ]中每個(gè)元素指示的學(xué)生順序打印學(xué)生姓名。例如∶student數(shù)組有3個(gè)學(xué)生∶Alex Jack Mei
index[ ]= { 1,2,0 }則打印結(jié)果為∶
Jack
Mei
Alex
注∶請(qǐng)勿打印除姓名外的其它字符,否則可能會(huì)導(dǎo)致錯(cuò)誤。
typedef struct student{
int number;//學(xué)號(hào)
char name[4];//姓名
float score; //成績(jī)
} stuType,* stuPtrType;
void printName(stuType student[ ],int index[ ],int n)
#include "allinclude.h"
void printName(stuType student[], int index[], int n)
{ // Add your code here
//int length = (sizeof(student)/sizeof(stuType));
//printf("%d",length);
for(int i=0;i<n;i++)
{
if (index[i]<n)
for(int j=0;j<4;j++)
printf("%c",student[index[i]].name[j]);
printf("\n");
}
}
7.DC01PE21
已知某表示"學(xué)生"的結(jié)構(gòu)體定義如下,編寫(xiě)算法,查找一個(gè)"學(xué)生"結(jié)構(gòu)體數(shù)組中的最高成績(jī)。
typedef struct student {
int number;//學(xué)號(hào)
char name【4】;// 姓名
float score; //成績(jī)
} stuType,* stuPtrType;
float highestScore(stuType *student[ ],int n)
/*返回最高成績(jī) */
#include "allinclude.h"
float highestScore(stuType *student[], int n)
/* 返回最高成績(jī) */
{ // Add your code here
float max_score=student[0]->score;
int i;
for( i=1;i<n;i++)
if(student[i]->score>max_score)
{
max_score=student[i]->score;
}
return max_score;
}
8.DC01PE22
已知某表示"學(xué)生"的結(jié)構(gòu)體定義如下,編寫(xiě)算法,打印"學(xué)生"結(jié)構(gòu)體數(shù)組中第一個(gè)最高成績(jī)學(xué)生的姓名和成績(jī)。注意∶(1)打印姓名后,要換行。
(2)打印成績(jī)后,要換行。
(3)只能使用printf函數(shù)進(jìn)行打印,其中成績(jī)只打印到小數(shù)點(diǎn)后2位,示例如下∶
Alex
99.99
(4)請(qǐng)勿打印除姓名外的其它字符,否則可能會(huì)導(dǎo)致錯(cuò)誤。
typedef struct student {
int number;//學(xué)號(hào)
char name[5]; // 姓名,最后一位是'\0',無(wú)需打印
float score;//成績(jī)
} stuType,* stuPtrType;
// 實(shí)現(xiàn)以下函數(shù)∶
void printFirstName_HighestScore(stuType *student[ ],int n);
#include "allinclude.h"
void printFirstName_HighestScore(stuType *student[], int n)
{ // Add your code here
float max_score=student[0]->score;
char max_name[4];
int i,temp=0;
for( i=1;i<n;i++)
{
if(student[i]->score > max_score)
{
max_score=student[i]->score;
temp=i;
}
}
strcpy(max_name,student[temp]->name);
//打印
for(i=0;i<4;i++)
printf("%c",max_name[i]);
printf("\n%.2f\n",max_score);
}
9.DC01PE23
同8.
#include "allinclude.h"
void printLastName_HighestScore(stuType *student[], int n)
{ // Add your code here
float max_score=student[0]->score;
char max_name[4];
int i,temp=0;
for( i=1;i<n;i++)
{
if(student[i]->score>=max_score)
{
max_score=student[i]->score;
temp=i;
}
}
strcpy(max_name,student[temp]->name);
//打印
for(i=0;i<4;i++)
printf("%c",max_name[i]);
printf("\n%.2f\n",max_score);
}
10.DC01PE25E
【例題】學(xué)習(xí)函數(shù)指針的定義與使用。
注意∶本題目是例題,無(wú)需修改添加任何代碼即可正常運(yùn)行。請(qǐng)注意編譯、運(yùn)行和調(diào)試本道題目,觀察執(zhí)行過(guò)程中的函數(shù)調(diào)用順序。
【程序】void A(){
printf("X\n");
}
void B(){
printf("Y\n");
}
int main()
{
void(*funcp)();//定義函數(shù)指針
funcp = A;
(*funcp)();// 實(shí)際上調(diào)用了A();
funcp = B;
(*funcp)();//實(shí)際上調(diào)用了B();
return 0;
}
#include "allinclude.h" //DO NOT edit this line
void A() {
printf("X\n");
}
void B() {
printf("Y\n");
}
int main()
{
void (*funcp)(); //定義函數(shù)指針
funcp = A;
(*funcp)(); // 實(shí)際上調(diào)用了A( );
funcp = B;
(*funcp)(); // 實(shí)際上調(diào)用了B( );
return 0;
}
11.DC01PE26E
【例題】函數(shù)指針作為函數(shù)的參數(shù)。
注意∶本題目是例題,無(wú)需修改添加任何代碼即可正常運(yùn)行。請(qǐng)注意編譯、運(yùn)行和調(diào)試本道題目,觀察執(zhí)行過(guò)程中的函數(shù)調(diào)用順序。
【程序】void hello()
{
printf("Hello wiorld!\n");
}
void runFun(void(*pFun)( ) )
{
pFun(); //函數(shù)指針
}
int main()
{
runFun(hello);
return 0;
}
#include "allinclude.h" //DO NOT edit this line
void hello()
{
printf("Hello world!\n");
}
void runFun(void (*pFun)())
{
pFun(); //函數(shù)指針;
}
int main()
{
runFun(hello); //hello是實(shí)際要調(diào)用的函數(shù)
return 0;
}
12.DC01PE30
編寫(xiě)程序,將結(jié)構(gòu)體中的字符串逆序放置。已知一個(gè)字符串結(jié)構(gòu)體定義如下∶
typedef struct {
ElemType* elem; //存放一個(gè)字符串
int length; //字符串的長(zhǎng)度
} StrSequence;
StrSequence* reverseStr(StrSequence* strSeq);
/*返回一個(gè)結(jié)構(gòu)體,該結(jié)構(gòu)體將strSeq中的字符串逆序存放*/
注意∶請(qǐng)勿修改strSeq中的任何值。
#include "allinclude.h"
StrSequence* reverseStr(StrSequence* strSeq)
/*返回一個(gè)結(jié)構(gòu)體,該結(jié)構(gòu)體將strSeq中的字符串逆序存放*/
{ // Add your code here
int i=0,length;
char temp;
for(;strSeq->elem[i++]!=0;)
;
length=--i;
for(i=0;i<=length/2;i++ )
if(strSeq->elem[i]!=strSeq->elem[length-i-1])
{
temp=strSeq->elem[i];
strSeq->elem[i]=strSeq->elem[length-i-1];
strSeq->elem[length-i-1]=temp;
}
return strSeq;
}
13.DC01PE49
【題目】試寫(xiě)一算法,由長(zhǎng)度為n的一維數(shù)組a構(gòu)建一個(gè)序列s。
要求實(shí)現(xiàn)下列函數(shù)∶
Status CreateSequence(Sequence &S,int n,ElemType *a);
/*由長(zhǎng)度為n的一維數(shù)組a構(gòu)建一個(gè)序列S,并返回OK。*/
/* 若構(gòu)建失敗,則返回ERROR*/
序列的結(jié)構(gòu)體定義為∶
typedef struct {
ElemType*elem;
int length;
}Sequence;
#include "allinclude.h" //DO NOT edit this line
Status CreateSequence(Sequence &S, int n, ElemType *a) {
// Add your code here
if(n<1) 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;
}
14.DC01PE61
【題目】鏈表的結(jié)點(diǎn)和指針類(lèi)型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode,*LinkList;
試寫(xiě)一函數(shù),構(gòu)建一個(gè)值為x的結(jié)點(diǎn)。
要求實(shí)現(xiàn)下列函數(shù)∶
LinkList MakeNode(ElemType x);
/* 構(gòu)建一個(gè)值為x的結(jié)點(diǎn),并返回其指針。 */
/* 若構(gòu)建失敗,則返回NULL。*/
?
#include "allinclude.h" //DO NOT edit this line
LinkList MakeNode(ElemType x) {
// Add your code here
LNode *p;
p = (LNode*)malloc(sizeof(LNode));
if(NULL != p)
p->data = x;
p->next = NULL;
return p;
}
15.DC01PE63
【題目】鏈表的結(jié)點(diǎn)和指針類(lèi)型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode,*LinkList;
試寫(xiě)一函數(shù),構(gòu)建長(zhǎng)度為2且兩個(gè)結(jié)點(diǎn)的值依次為x和y的鏈表。
要求實(shí)現(xiàn)下列函數(shù)∶
LinkList CreateLinkList(ElemType x,ElemType y);
/* 構(gòu)建其兩個(gè)結(jié)點(diǎn)的值依次為x和y的鏈表。*/
/* 若構(gòu)建失敗,則返回NULL。*/
#include "allinclude.h" //DO NOT edit this line
LinkList CreateLinkList(ElemType x, ElemType y) {
// Add your code here
LinkList head= (LinkList)malloc (sizeof (LNode));
if(head)
{
head->data=x;
head->next= (LinkList)malloc (sizeof (LNode));
if(head->next)
{ head->next->data=y;
head->next->next=NULL;
}
else return NULL;
}
return head; // This is a temporary code. Change it if necessary.
}
16.DC01PE65
【題目】鏈表的結(jié)點(diǎn)和指針類(lèi)型定義如下
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode,*LinkList;
試寫(xiě)一函數(shù),構(gòu)建長(zhǎng)度為2的升序鏈表,兩個(gè)結(jié)點(diǎn)的值分別為x和y,但應(yīng)小的在
前,大的在后。要求實(shí)現(xiàn)下列函數(shù)∶
LinkList CreateOrdLList(ElemType x,ElemType y);
/* 構(gòu)建長(zhǎng)度為2的升序鏈表。*/文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-730979.html
/*若構(gòu)建失敗,剛返回NULL。*/文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-730979.html
#include "allinclude.h" //DO NOT edit this line
LinkList CreateOrdLList(ElemType x, ElemType y) {
// Add your code here
LinkList head= (LinkList)malloc (sizeof (LNode));
if(head)
{
head->next= (LinkList)malloc (sizeof (LNode));
if(head->next)
{
head->next->next=NULL;
}
else return NULL;
}
if(x<y)
head->data=x,head->next->data=y;
else
head->data=y,head->next->data=x;
return head; // This is a temporary code. Change it if necessary.
}
到了這里,關(guān)于廣工anyview數(shù)據(jù)結(jié)構(gòu)第一章(2021.12)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!