广工AnyView数据结构Word文档下载推荐.docx
《广工AnyView数据结构Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《广工AnyView数据结构Word文档下载推荐.docx(43页珍藏版)》请在冰豆网上搜索。
c)swap(b,c);
}
voidswap(int&
a,int&
b){
inttemp;
temp=a;
a=b;
b=a;
题目】试编写算法求一元多项式
P(x)=a0+a1x+a2x^2+...+anx^n的值P(x0),并确定算法中每一语句的执行次数和整个算法的时间复杂度。
**********/
floatPolynomial(intn,inta[],floatx)
/*求一元多项式的值P(x)。
*/
/*数组a的元素a[i]为i次项的系数,i=0,...,n*/
{
floatanswer=a[0];
floattemp=1.0;
for(inti=1;
i<
=n;
i++)
temp*=x;
answer+=a[i]*temp;
returnanswer;
精选
*********
题目】已知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,...试编写求k阶裴波那契序列的第m项值的函数算法,k和m均以值调用的形式在函数参数表中出现。
**********/
StatusFibonacci(intk,intm,int&
f)
/*求k阶斐波那契序列的第m项的值f*/
if(k<
=1||m<
0)
returnERROR;
elseif(m==k-1)
f=1;
elseif(m==0)
f=0;
else
inti,j,sum;
int*t;
t=(int*)malloc(m*sizeof(int));
for(i=0;
=k-2;
t[i]=0;
t[k-1]=1;
for(i=k;
=m;
{sum=0;
for(j=i-k;
j<
=i;
j++)
sum+=t[j];
t[i]=sum;
f=t[m];
returnOK;
【题目】试编写算法,计算i!
×
2^i的值并存入数组a[0..n-1]的第i-1个分量中(i=1,2,⋯,n)。
假设计算机中允许的整数最大值为MAXINT,则当对某个k(1≤k≤n)使k!
2^k>
MAXINT时,应按出错处理。
注意选择你认为较好的出错处理方法。
StatusSeries(inta[],intn)
/*求i!
*2^i序列的值并依次存入长度为n的数组a;
/*若所有值均不超过MAXINT,则返回OK,否则OVERFLOW*/{
intt=1,p=1;
t*=i;
p*=2;
a[i-1]=t*p;
if(a[i-1]>
MAXINT)returnERROR;
【题目】假设有A、B、C、D、E五个高等院校进行田径对抗赛,各院校的单项成绩均以存入计算机并构成一张表,表中每一行的形式为:
项目名称性别校名成绩得分编写算法,处理上述表格,以统计各院校的男、女总分和团体总分,并输出。
voidScores(ResultType*result,ScoreType*score)
/*求各校的男、女总分和团体总分,并依次存入数组score*/
/*假设比赛结果已经储存在result[]数组中,*/
/*并以特殊记录{"
"
male,'
'
"
0}(域scorce=0)*/
/*表示结束*/
inti=0;
while(result[i].sport!
=NULL)
switch(result[i].schoolname)
case'
A'
:
score[0].totalscore+=result[i].score;
if(result[i].gender==male)
score[0].malescore+=result[i].score;
score[0].femalescore+=result[i].score;
break;
B'
score[1].totalscore+=result[i].score;
score[1].malescore+=result[i].score;
score[1].femalescore+=result[i].score;
C'
score[2].totalscore+=result[i].score;
if(result[i].gender==male)score[2].malescore+=result[i].score;
score[2].femalescore+=result[i].score;
D'
score[3].totalscore+=result[i].score;
score[3].malescore+=result[i].score;
score[3].femalescore+=result[i].score;
E'
score[4].totalscore+=result[i].score;
if(result[i].gender==male)score[4].malescore+=result[i].score;
score[4].femalescore+=result[i].score;
}
i++;
【题目】试写一算法,对序列S的第i个元素赋以值e。
序列的类型定义为:
typedefstruct{
ElemType*elem;
intlength;
}Sequence;
StatusAssign(Sequence&
S,inti,ElemTypee)
/*对序列S的第i个元素赋以值e,并返回OK。
/*若S或i不合法,则赋值失败,返回ERROR*/
if(S.length<
1||i>
S.length)
S.elem[i]=e;
序列的类型定义为:
StatusCreateSequence(Sequence&
S,intn,ElemType*a)
/*由长度为n的一维数组a构建一个序列S,并返回OK。
/*若构建失败,则返回ERROR*/
if(n<
1)
S.elem=(ElemType*)malloc(n*sizeof(ElemType));
S.elem[0]=a[0];
n;
S.elem[i]=a[i];
S.length=n;
【题目】链表的结点和指针类型定义如下
typedefstructLNode{
ElemTypedata;
structLNode*next;
}LNode,*LinkList;
试写一函数,构建一个值为x的结点。
***********/
LinkListMakeNode(ElemTypex)
/*构建一个值为x的结点,并返回其指针。
*//*若构建失败,则返回NULL。
*/{
LNode*p;
p=(LNode*)malloc(sizeof(LNode));
if(p==NULL)returnNULL;
p->
data=x;
returnp;
题目】链表的结点和指针类型定义如下
typedefstructLNode{ElemTypedata;
试写一函数,构建长度为2且两个结点的值依次为x和y的链表。
LinkListCreateLinkList(ElemTypex,ElemTypey)/*构建其两个结点的值依次为x和y的链表。
if(p==NULL)returnNULL;
{p->
next=(LNode*)malloc(sizeof(LNode));
if(p->
next==NULL)returnNULL;
p->
next->
data=y;
next=NULL;
【题目】链表的结点和指针类型定义如下typedefstructLNode{ElemTypedata;
试写一函数,构建长度为2的升序链表,两个结点的值分别为x和y,但应小的在前,大的在后。
LinkListCreateOrdLList(ElemTypex,ElemTypey)
/*构建长度为2的升序链表。
/*若构建失败,则返回NULL。
if(p==NULL)
returnNULL;
next==NULL)
data=(x<
y)?
x:
y;
data=(x>
【题目】试写一算法,实现顺序栈的判空操作
题目】试写一算法,实现顺序栈的取栈顶元素操作
GetTop_Sq(SqStackS,ElemType&
e。
)顺序栈的类型定义