广工数据结构参考答案全(anyview)分析

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

广工数据结构anyview 80道上机题

1.

void Descend(int &x, int &y, int &z)

/* 按从大到小顺序返回x,y和z的值*/ {

int t;

if(x

{

t=z;

z=x;

x=t;

}

if(y

{

t=y;

y=z;

z=t;

}

if(y>x)

{

t=x;

x=y;

y=t;

}

}

2.

Status Fibonacci(int k, int m, int &f)

/* 求k阶斐波那契序列的第m项的值f */ {

int *a;

int i=1;

if(k<2||m<0) return ERROR;

if(m

{

if(m==k-1) f=1;

else f=0;

return OK;

}

a=(int*)malloc((m+1)*sizeof(int));

for(i=0;i

i=k+1;

a[k-1]=1;

a[k]=1;

while(i<=m)

{

a[i]=2*a[i-1]-a[i-k-1];

i++;

}

f=a[m];

return OK;

}

3.

void Scores(ResultType *result, ScoreType *score)

/* 求各校的男、女总分和团体总分, 并依次存入数组score */

/* 假设比赛结果已经储存在result[ ]数组中, */

/* 并以特殊记录{"", male, ' ', "", 0 }(域scorce=0)*/

/* 表示结束*/

{

int i;

for(i=0;result[i].score!=0;i++)

{

score[result[i].schoolname-'A'].totalscore+=result[i].score;

if(result[i].gender==male)

score[result[i].schoolname-'A'].malescore+=result[i].score;

else

score[result[i].schoolname-'A'].femalescore+=result[i].score;

}

}

4

Status Series(int ARRSIZE, int a[])

/* 求i!*2^i序列的值并依次存入长度为ARRSIZE的数组a;*/

/* 若所有值均不超过MAXINT,则返回OK,否则返回OVERFLOW */

{

int i=1,b=1,na=1;

while(i<=ARRSIZE)

{

na*=i;b*=2;

if(na*b>MAXINT) return OVERFLOW;

a[i-1]=na*b;

i++;

if(i>ARRSIZE+1) return OVERFLOW;

}

return OK;

}

5

float Polynomial(int n, int a[], float x)

/* 求一元多项式的值P(x)。*/

/* 数组a的元素a[i]为i次项的系数,i=0,...,n */

{

float ans=a[0],t=1.0;

int i=1;

while(i<=n)

{

t*=x;

ans+=(t*a[i]);

i++;

}

return ans;

}

6

void InsertOrderList(SqList &L, ElemType x)

// 在有序的顺序表L 中保序插入数据元素x

{

int i=L.length-1;

while(L.elem[i]>x)

{

L.elem[i+1]=L.elem[i];

i--;

}

L.elem[i+1]=x;

L.length++;

}

7

char Compare(SqList A, SqList B)

// 比较顺序表A和B,

// 返回'<', 若A

// '=', 若A=B;

// '>', 若A>B

{

int i=0;

while(A.elem[i]==B.elem[i]&&i

if(i==A.length&&i==B.length) return '=';

if(A.elem[i]>B.elem[i]||i==B.length) return '>';

return '<';

}

8

LinkList Locate(LinkList &L, ElemType x)

// If 'x' in the linked list whose head node is pointed

// by 'L', then return pointer ha pointing node 'x',

相关文档
最新文档